Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

felix thomas
Ranch Hand
Posts: 89
hi,

please any anyone help me to convert hexadecimal and octal values to decimal
i know the vice a versa i.e. from decimal to hexadecimal ,octal

Junilu Lacar
Bartender
Posts: 7317
45
For Decimal to Hex, just divide successively by 16, keeping the whole number result but noting the remainder of each division as you go, until you get a 0 result.
Take decimal 11948
11948 / 16 = 746 rem. 12
746 / 16 = 46 rem. 10
46 / 16 = 2 rem 14
2 / 16 = 0 rem 2
Then, go backwards through your list of remainders and convert each to hex. For this example, you will get 2EAC, which is the Hex representation of Decimal 11948
Same process when you convert Decimal to Octal, only your divisor will be 8.
For Hex to Decimal:
2EAC = (2 * 16^3) + (14 * 16^2) + (10 * 16^1) + (12 * 16^0) = 11948
When converting from Octal, use powers of 8.

[This message has been edited by JUNILU LACAR (edited June 21, 2001).]

Manfred Leonhardt
Ranch Hand
Posts: 1492
Hi Felix,
Its easy, as long as you can perform a power function and some multiplication.
Hex to Dec:
Values 0 to 9 translate directly, A - F map to 10 to 15
Generic formula:
Decimal subValue = Hex digit * (16 ^ digit position)
where digit position starts at zero in the rightmost position and increments by one as you go left.
Once you get all the subtotals then you just add them together.
Examples:
0x4321
1*16^0 + 2*16^1 + 3*16^2 + 4*16^4
1*1 + 2*16 + 3*256 + 4*4096
1 + 32 + 768 + 16384
17185
0x7D
13*16^0 + 7*16^1
13*1 + 7*16
125
Oct to Dec:
Use the same formula but you use 8 instead of 16.
Values 0 - 7 translate directly

Decimal subValue = Oct digit * (8 ^ digit position)
Examples:
076
6 * 8^0 + 7 * 8^1
6*1 + 7*8
6 + 56
62
0432
2 * 8^0 + 3 * 8^1 + 4 * 8^2
2*1 + 3*8 + 4*64
2 + 24 + 256
282
Regards,
Manfred.

Jane Griscti
Ranch Hand
Posts: 3141
Hi Felix,
I have some notes on binary,octal,decimal, hexidecimal conversions posted at http://www.janeg.ca/scjp/oper/binhex.html
Hope they help.
------------------
Jane Griscti
Sun Certified Programmer for the Javaï¿½ 2 Platform

felix thomas
Ranch Hand
Posts: 89
hi,
thanks a lot guys