# Decimal to Hexadecimal, Without Java Libraries.

• February 2nd, 2012, 07:10 PM
ribmant
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.

Code :

```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(?); }```
• February 3rd, 2012, 12:31 AM
Mr.777
Re: Decimal to Hexadecimal, Without Java Libraries.
Are there any problems with the code?
• February 3rd, 2012, 06:28 AM
Norm
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);