This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Refactoring for Software Design Smells: Managing Technical Debt this week in the OO, Patterns, UML and Refactoring forum!

# hex to dec

Netty poestel
Ranch Hand

Joined: Sep 20, 2004
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

Joined: Oct 02, 2003
Posts: 11855

18

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 ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Super...Fred
Kosh Shah
Greenhorn

Joined: Jul 20, 2004
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!

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com

subject: hex to dec