• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Hexadecimal and decimal convert code

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Qing, thats the right method to convert from hex to dec.
 
Qing Tian
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all for help, that will help me alot!
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a 'manual' way

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

 
Qing Tian
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Michael that you give me more idea on these codes.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic