Welcome to Dream.In.Code
Become a Java Expert!

Join 149,617 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,856 people online right now. Registration is fast and FREE... Join Now!




HEXADECIMAL CONVERTER

 
Reply to this topicStart new topic

HEXADECIMAL CONVERTER, create program that will accept 4digit hexadecimal

kshiman
19 Sep, 2007 - 07:43 PM
Post #1

New D.I.C Head
*

Joined: 19 Sep, 2007
Posts: 8


My Contributions
I am trying to write a program that wil accept a 4-digit hexadecimal number as input from the user and then output the decimal value in numeral form.

I have no idea what I am doing, I keep getting errors all over the place and this is my first time working with java.
I am suppose to use the scanner class to get a hexadecimal number and save it as a string value...but i have no idea how to do that. this is what i tried below....no clue if its even in the right boat.

any help would be appreciative.
Thank You


CODE
public class HextoDecimal {

    public static int hex2decimal(String s) {
      int value = Integer.parseInt(hexValue, 16);  
      hexValue=ABCD
     ;String digits = "0123456789ABCDEF";
        s = s.toUpperCase();
        int val = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            int d = digits.indexOf(c);
            val = 16*val + d;
        }
        return val;
    }
}

User is offlineProfile CardPM
+Quote Post

goldenthunder
RE: HEXADECIMAL CONVERTER
20 Sep, 2007 - 12:02 AM
Post #2

New D.I.C Head
*

Joined: 18 Sep, 2007
Posts: 13


My Contributions
ok..I copied your code to eclipse and these are the syntax errors shown before compilation:


CODE
int value = Integer.parseInt(hexValue, 16);  
      hexValue=ABCD;


I dont know what's that fore, because its never used, but the corrrect syntax would be:
CODE
String hexValue="ABCD";
int value = Integer.parseInt(hexValue, 16);  
      


next: to test it I added a main function (not neccessary if you're using this class from another class):
CODE
public static void main(String[] args) {
        System.out.print(hex2decimal("A"));
    }

works fine.

EDIT:
For the Scanner class, have a look on one of the apis:
http://java.sun.com/reference/api/index.html



This post has been edited by goldenthunder: 20 Sep, 2007 - 12:09 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:34AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month