Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hex to dec Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hex to dec" Watch "hex to dec" New topic
Author

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: 11170
    
  16

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!
 
 
subject: hex to dec
 
Similar Threads
Big-O Notation??? (Don't understand)
Different ways of Array creation
rant: Scrolling Websites
Programming Exercises :)
What are you going to eat for dinner today?