Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

how to convert decimals to octal and hexadecimal?

Karthik Gurunathan
Greenhorn
Posts: 19
Hi there ,
I am still confused as to how to convert the decimals to octal values and hexadecimal values in JAVA ??? please help.
Thanks,
karhik

Ranch Hand
Posts: 170
Hi Karthik,
In Java language you can do that using he methods
toOctalString(),toHexString(),toBinaryString methods of the Integer and Long classes.
Or are you looking for the general rules of conversion?

Karthik Gurunathan
Greenhorn
Posts: 19
Hi sekar,
I understand that ,...I am looking for general method of conversion ..exam point of view..if they ask like whats the hexadecimal equivalent for some particular decimal..stuff like that...
karthik

Sandhya Harish
Greenhorn
Posts: 22
to convert a decimal to octal :
say the decimal number was 70
- 70/8 = 8, remainder = 6
- 8/8 = 1, remainder = 0
- 1/8 = 0, remainder = 1
therefore, octal eqv of 70 is 0106, u can recheck this by
0*8^3 + 1*8^2 + 0*8^1 + 6*8^0 = 0+64+0+6 = 70
to convert a decimal to hexadecimal:
say the decimal number is 125
- 125/16 = 7, remainder = D(13)
- 7/16 = 0, remainder = 7
therefore, hexadecimal equivalent of 125 is 7D, u can recheck this by
7*16^1 + 13*16^0 = 112+13 = 125.
hope this helped!!

Vicken Karaoghlanian
Ranch Hand
Posts: 522
Karthik-
What you are asking is a very general question, I did a google search for you and came up with lots of hits, they are all worth checking.
How to convert between different base types
If you have a more specific question, we will gladly answer it.