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, Without Java Libraries.

Hello, so I have to create a program in java that does not use any automatic converting libraries. I've completed decimal to binary, and now im stuck on decimal to hexadecimal. The code i've wrote for this is below. Any comments would be greatly appreciated.

```public void getDec()
{
String Dec;
Dec = JOptionPane.showInputDialog("Please enter the decimal number you would like to convert");
num = Integer.parseInt(Dec);
}

public void decToHex()
{
getDec();               //ask user for a decimal
int hex[] = new int[8]; //container for new hexadecimal
int i, j = 0;

while (num != 0)        //base 16 code
{
num = num / 16;
hex[i]=num;
hex[i] = hex[i] % 16;
i++;
}
while(hex[j]>=10)
{
switch(hex[j])
{
case 10: hex[j] = 'A';
j--;
break;
case 11: hex[j] = 'B';
j--;
break;
case 12: hex[j] = 'C';
j--;
break;
case 13: hex[j] = 'D';
j--;
break;
case 14: hex[j] = 'E';
j--;
break;
case 15: hex[j] = 'F';
j--;
break;
}
}
//System.out.println(?);
}```

2. ## Re: Decimal to Hexadecimal, Without Java Libraries.

Are there any problems with the code?

3. ## Re: Decimal to Hexadecimal, Without Java Libraries.

Instead of a switch statement you could use an array.
Or consider that chars can be used in arithmetic statements:
int value = 'A' + (nbr - 10);