Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. decimal to hexadecimal

Hi, i am still practicing with my java programming, and i decided to write a code today that would convert decimal numbers to hexadecimal numbers. Unfortunately, it does not compile. The error message is "cannot find symbol - variable A", and might be the same error for the other variables B,C,D,E,F. Could you please tell me what is wrong and why?
Here is my code:

public class dec2hex
{
public static void main (String [] args)
{int nbr = 99;
char a = A;
char b = B;
char c = C;
char d = D;
char e = E;
char f = F;
while (nbr<999)
{
int x = nbr%16;
nbr = nbr - x;
int y = nbr/16;
if (x==10)
{x=(char)a;}
if (x==11)
{x=(char)b;}
if (x==12)
{x=(char)c;}
if (x==13)
{x=(char)d;}
if (x==14)
{x=(char)e;}
if (x==15)
{x=(char)f;}
if (y==10)
{y=(char)a;}
if (y==11)
{y=(char)b;}
if (y==12)
{y=(char)c;}
if (y==13)
{y=(char)d;}
if (y==14)
{y=(char)e;}
if (y==15)
{y=(char)f;}
}
System.out.println ("le résultat est");
System.out.print (y);
System.out.println (x);

}
}

Thank you for your help, it will be very much appreciated   Reply With Quote

3. Re: decimal to hexadecimal

char a = A;

This is attempting to assign the primitive variable a to the value of another variable A. You can define a character literal with the ' symbol.

char a = 'A';  Reply With Quote

5. Re: decimal to hexadecimal

Hi again, i did this to the code, it compiles perfectly, i added a lot of lines but basically they do the same thing over and over again. unfortunately, the code doesn't work for some values. for instance, instead of printing "2B" for the number 43, it prints 266. I have tried to fix this by multiple different ways but i cn't seem to make it work. Here is the code:

public class dec2hex
{
public static void main (String [] args)
{int nbr = 43;
char a = 'A';
char b = 'B';
char c = 'C';
char d = 'D';
char e = 'E';
char f = 'F';
if (nbr<999)
{
int x = nbr%16;
nbr = nbr - x;
int y = nbr/16;
if (x==0)
{
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}

if (x==1)
{
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}

if (x==2)
{
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}

if (x==3)
{
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}

if (x==4)
{
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}

if (x==5)
{
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}

if (x==6)
{
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}

if (x==7)
{
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}

if (x==8)
{
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}

if (x==9)
{
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}
if (x==10)
{x=(char)a;
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}
if (x==11)
{x=(char)b;
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}
if (x==12)
{x=(char)c;
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}
if (x==13)
{x=(char)d;
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}
if (x==14)
{x=(char)e;
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}
if (x==15)
{x=(char)f;
if (y==10)
{y=(char)a;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==11)
{y=(char)b;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==12)
{y=(char)c;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==13)
{y=(char)d;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==14)
{y=(char)e;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
if (y==15)
{y=(char)f;System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);}
else
{
System.out.println ("le resultat est:");
System.out.print (y);
System.out.print (x);
}
}

}
}

}  Reply With Quote

6. Re: decimal to hexadecimal Originally Posted by "stylelink
i added a lot of lines but basically they do the same thing over and over again
When you find yourself doing this ... stop and think about what you are trying to accomplish. Computers are excellent at repetition and loops are a fundamental concept for all programming languages.

What you need is pseudo-code or an algorithm. Can you write out in a step by step fashion how to convert a base 16 hexadecimal value into a base 10 decimal one?  Reply With Quote

8. Re: decimal to hexadecimal

thanks again, i decided to do something differently, but for that, i need to be able to call a separate class for help (basically, i write a class that would assign values to the 10/11/12/13/14/15 numbers, and call it out in my main class when needed), but i have no idea how to do that.  Reply With Quote

9. Re: decimal to hexadecimal Originally Posted by stylelink thanks again, i decided to do something differently,
Good for you! Trying something then learning from the mistakes and trying something else is the best way to learn  Originally Posted by stylelink
but for that, i need to be able to call a separate class for help
Now you are on the right track! But instead of writing something from scratch why not look for something that already exists? The Integer class is a good place to start.

(Okay.. a quick extra hint because the terminology will be difficult to a non native english speaker. The word 'radix' is a programmers term for the base of the integer.)  Reply With Quote

10. Re: decimal to hexadecimal

what i was thinking is just writing a class separately, which would take a number from 10 to 15 and return its associated letter, here from A to F. But i have no idea how to call it and make it work. If the class name was "Asso", how would i make it work in my converter? would i call it like:
" Asso(y) " for y the variable that i inputed? or another way?

PS: My goal here is to create the converter, in the link you sent, there already is a converter, but i would rather not read it until i am finished (also i'm pretty sure it will be way cleaner/efficient than the one i am working on).  Reply With Quote

11. Re: decimal to hexadecimal Originally Posted by stylelink what i was thinking is just writing a class separately, which would take a number from 10 to 15 and return its associated letter, here from A to F.
The approach is faulty. Hexadecimal is just a way of expressing a number in a different base. It's incorrect to look at a character and return what that character would be in decimal. You need to an algorithm to convert bases. Originally Posted by styelink
But i have no idea how to call it and make it work. If the class name was "Asso", how would i make it work in my converter? would i call it like:
" Asso(y) " for y the variable that i inputed? or another way?
Asso(y) would be the constructor for the object. You would create a member variable and assign it to the value of y. Then you would have a method of Asso that returns the converted result.

public class Asso {
private String _hex;

public Asso(String hexValue) {
_hex = hexValue;  //Assign the member variable
}

public int convertToDecimal() {
// Convert base 16 to base 10
return result;
}
}

Then when you want to do a conversion

Asso asso = new Asso("4E2A");
int result = asso.convertToDecimal();

// Check the result against the one provided with Integer.parse
if (result == (Integer.parse("4E2A", 16)) {
System.out.println("My algorithm is correct");
} Originally Posted by stylelink PS: My goal here is to create the converter, in the link you sent, there already is a converter, but i would rather not read it until i am finished (also i'm pretty sure it will be way cleaner/efficient than the one i am working on).
There's nothing wrong with implementing your own converter for a learning exercise but it's a good idea to use the parse method to verify your results.  Reply With Quote

12. The Following User Says Thank You to ChristopherLowe For This Useful Post: 