Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Hexadecimal and decimal convert code

Greenhorn
Posts: 10
please someone can teach me how to write a java code on convert hexadecimal to decimal and the decimal to hexadecimal.
which is the code not using following tools:
int i = 29;
String octal = Integer.toOctalString(i);
String hex = Integer.toHexString(i);
String binary = Integer.toBinaryString(i);

thanks

Ranch Hand
Posts: 1923
How would you convert the decimal 132 to hex by hand/ on paper?
How would you convert a7 to decimal without pc?

Qing Tian
Greenhorn
Posts: 10
on paper that I know
convert 132 to hex is:
132/16 = 8 remainder 4, so the hex will be 84

convert a7 to decimal is:
10(a)* 16 + 7 * 1= 167

I was confused if the number like 24032 and by hand the steps will be:
24032/16=1502 r 0
1502/16 = 93 r 14 (E)
93/16 = 5 r 13 (D)
5/16 = 0 r 5
the hex will be 5DE0
in the java code how the loop divide 16 untill decimal=0 and also the output can display like 5DE0 in order?
(I start learn java on 3 weeks ago so )
did I'm right on calculate? thanks for your help.
[ August 18, 2004: Message edited by: Qing Tian ]

Ranch Hand
Posts: 36
Hi there!

I was looking into the same problem myself not so long ago! Hard to find the solution, isn't it?!!! Behind the .toHexString etc. all that's happening is:
Convert int to Hex:

To go back to int from hex:

Similarly to convert from int to Binary String:

To go back to int from hex:

Hope this helps... By the way, i find it easier (less cumbersome) to convert to Binary before converting to Hex. When you get the binary number, you're then just converting every four bits to the Hex equivalent...

Cheers,
Celine

Greenhorn
Posts: 3
Qing, thats the right method to convert from hex to dec.

Qing Tian
Greenhorn
Posts: 10
thank you all for help, that will help me alot!

Ranch Hand
Posts: 4632
Here's a 'manual' way

for args[] enter eg
24032 10 16
or
5DE0 16 10

Qing Tian
Greenhorn
Posts: 10
thank you Michael that you give me more idea on these codes.

 Consider Paul's rocket mass heater.