x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 289

# Converting from hexadecimal using two's compliment

What are the steps you would take to convert a hexadecimal number such as 0x0AB00000 to a decimal number, knowing that it is an unsigned integer. Are there different steps to convert this number if you knew that it was two's compliment instead of an unsigned integer?
0
pcomb
1 Solution

Commented:
Yes,

The first step is the same either way as you need to convert the hex number to binary

00001010 10110000 00000000 00000000

This converts to a positive decimal value of 179306496

For a two's compliment, the most significant bit defines the sign, in this case, its 0 so its a positive number.

If we ignored your leading 0 in the hex string, and it was AB00000 then the first 4 0's are now not present and the most significant bit is now a 1 indicating a negative number.

The decimal value of the remaining bits is 45088768, and to get the negative value we subtract this value from 2^N where N is the number of bits in the original number (28)

2^28 = 268435456

so number = 268435456 - 45088768 = -223346688

Using a simpler example

10000000 = 2^8 - 0 = -128
11111111 = 2^8 - 127 = -1
0

Commented:
\$ echo 'ibase=16 ; ob=10 ; 0AB00000' | bc
179306496
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.