| Author |
Hexadecimal to decimal
|
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 624
|
|
How can I convert 0xDeadCafe to decimal and 1111 1000 0000 0000 0000 0000 0000 0000 to decimal. Sorry am very weak at Mathematics Thanks.
|
Be Humble... Be Nice.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
How can I convert 1111 1000 0000 0000 0000 0000 0000 0000 to decimal
Java integers uses Twos Complement format. First, the sign bit is set, so the number is negative. For negative numbers, to figure out the number you need to negate the number to a positive number. And then calculate that positive number, as usual. X = 1111 1000 0000 0000 0000 0000 0000 0000 ~X = 0000 0111 1111 1111 1111 1111 1111 1111 ~X + 1 = -X = 0000 1000 0000 0000 0000 0000 0000 0000 -X = 134217728 X = -134217728 For more information, try google "twos complement". Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
How can I convert 0xDeadCafe to decimal
First, convert the hexidecimal to binary, and then convert the binary to decimal. DEADCAFE = 1101 1110 1010 1101 1100 1010 1111 1110 To convert to decimal, as with the previous post, you need to use Twos Complement. [Note: You can go directly from Hexidecimal to Decimal, but you should confirm that it is a positive number first] Henry [ September 17, 2008: Message edited by: Henry Wong ]
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
In hex (base 16), the position furthest to the right represents 16^0 (meaning 16 raised to the 0 power). The position left of that represents 16^1. The next position left of that represents 16^2. And so on. Also, the characters a, b, c, d, e, and f are used to represent decimal (base 10) values 10, 11, 12, 13, 14, and 15 respectively. So for example, 0xb32 means: (2 * 16^0) + (3 * 16^1) + (b * 16^2) ...which is... (2 * 1) + (3 * 16) + (11 * 256) ...which is 2 + 48 + 2816 = 2866 in decimal.
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 624
|
|
Thanks Henry.. I will learn twos compliment now Um... Marc, thanks for replying too...does lower case and upper case letters matter? Can I consider same lower case d as 13 and Upper case D as 13 too? [ September 17, 2008: Message edited by: Arjun Reddy ]
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
Originally posted by Arjun Reddy: Thanks Henry.. I will learn twos compliment now Um... Marc, thanks for replying too...does lower case and upper case letters matter? Can I consider same lower case d as 13 and Upper case D as 13 too?
You might want to review your base mathematics. Hexidecimal numbers are just base 16 numbers. But to answer your question, d = 13, whether it is lower or upper case. Henry
|
 |
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 624
|
|
The reason I asked was because I was confused, the answer for 0xDeadCafe is -559035650.This is not the result I get according to what Marc said. Doing just 13*(16^7) itself is giving a number bigger than the answer. Thanks.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
Originally posted by Arjun Reddy: The reason I asked was because I was confused, the answer for 0xDeadCafe is -559035650.This is not the result I get according to what Marc said. Doing just 13*(16^7) itself is giving a number bigger than the answer. Thanks.
That is because the number is negative -- the sign bit is one. You need to take into account twos complement when negative numbers are involved. Henry
|
 |
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 624
|
|
|
Okie Henry and Marc Thanks a lot for answering.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
Try it with 0xFE first; it is a lot easier to handle 8 bits than 32!
|
 |
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 624
|
|
Originally posted by Henry Wong: That is because the number is negative -- the sign bit is one. You need to take into account twos complement when negative numbers are involved. Henry
Henry, Then for 0xbeef, the sign bit is also 1(b-->1011) right, so we have to do a twos complement right? but the answer to this is 48879(twos complement not done here)? Thanks. [ September 20, 2008: Message edited by: Arjun Reddy ]
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
Henry, Then for 0xbeef, the sign bit is also 1(b-->1011) right, so we have to do a twos complement right? but the answer to this is 48879(twos complement not done here)?
Is 0xbeef a short? Or is it an int? If it is a short, then the sign bit is one... ie. the number is negative. And the twos complement should yield an answer of -16657. If it is an int, then the sign but is zero... because the full number is 0x0000beef. And the number is 48879. Remember, the sign bit is the last bit, which is dependent on the size of the type. Henry [ September 20, 2008: Message edited by: Henry Wong ]
|
 |
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 624
|
|
|
Okie.. I got it. Thanks Henry.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
Originally posted by Campbell Ritchie: Try it with 0xFE first; it is a lot easier to handle 8 bits than 32!
And I agree with Campbell. Work with 8 bits -- as it is much easier to do the twos complement math on. (which should make it easier to learn twos complement) But as you have seen, if you are going to confirm the results, makes sure that you cast the type before you print it. As 0xFE may be a byte, but until you cast it, it is considered an int literal. Henry
|
 |
 |
|
|
subject: Hexadecimal to decimal
|
|
|