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

# hex to dec

Netty poestel
Ranch Hand
Posts: 131
int k = 0x12 ;
is 1 time 16, and left over 2 = 18

similiarly 0x42,
4 times 16 and leftover of 2 = 66

how does one take the same algorithm for say[0x892]

8 times 16 and leftover 92 does not work
89 times 16 and leftover 2 does not work
and neither does 892 times 16.

so what's the catch here ?

TIA

fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
what does 892 mean in decimal? it does it mean 8*10 plus 9*10 with 2 left over?

no. each position represents a POWER of (in decimal) 10...
10^0 = 1
10^1 = 10
10^2 = 100

8*100 + 9*10 + 2*1

Hex works the exact same way, except with a base of 16, not 10.

so, you need to know the powers of 16...
16^0 = 1
16^1 = 16
16^2 = 256

so 0x892 means 8*256 + 9*16 + 2*1
[ October 02, 2004: Message edited by: fred rosenberger ]

Netty poestel
Ranch Hand
Posts: 131
Super...Fred

Kosh Shah
Greenhorn
Posts: 19
As an example,

If you have a hexadecimal value: 0x1234

Its decimal equivalent is: (starting from the right side, or the Least Significant Bit - LSB)
4*(16^0) + 3*(16^1) + 2*(16^2) + 1*(16^3)
= 4*1 + 3*16 + 2*256 + 1*4096
= 4 + 48 + 512 + 4096
= 4660

Hope this helps!

Cheers,
Kosh!