# HEX and Binary Numbers

• Proficient
• Posts: 379

3+ Months Ago

Hi All

I am hoping someone can give me some explanations on a situation I have.

I will give examples as this is the best way to explain.

I take a decimal number, 401, and convert to 8-character Hex which gives me 00000191
I then break down the hex number into 4 groups of 2 and convert them to binary.
00 -> 0
00 -> 0
01 -> 1
91 -> 10010001

I then CONCATENATE the non-zero binary to get 110010001
If I do a direct Hex to binary using the whole Hex get 110010001,the same as when I broke it down.

Now I do the same for decimal 278
D 278 -> H 00000116

00 -> 0
00 -> 0
01 -> 1
16 -> 10110

CONCATENATE the non-zero binary to get 110110
Direct Hex to binary gets 100010110, which is correct, but not the same.

I tried decimal numbers like 65 and 250 they worked fine.
I tried decimal numbers like 300 and it doesn't work.

If I split the Hex number into 2 groups of 4, then everything is fine.
The problem is that using 2 groups of 4, my max Hex number is limited to < H 200.

What it boils down to is that my method works most of the time, but not all the time, which to me is the same as not working as I will get wrong answers.

Anybody have any ideas.

Thanks
• Brewmaster
• Posts: 6296
• Loc: Seattle, WA

3+ Months Ago

Leading zeros matter. You're dropping significant digits in your conversion to binary:

Quote:
Now I do the same for decimal 278
D 278 -> H 00000116

00 -> 0
00 -> 0
01 -> 1
16 -> 10110

CONCATENATE the non-zero binary to get 110110
Direct Hex to binary gets 100010110, which is correct, but not the same.

Don't discard the 0's in your binary conversions; always write them out to a full 8-bit number. When you convert 0x16 to binary, you're leaving out important leading zeros from the number:

D 278 -> H 00000116

00 -> 0000 0000
00 -> 0000 0000
01 -> 0000 0001
16 -> 0001 0110

This gives us: 0000 0000 0000 0001 0001 0110

Result (dropping leading 0's now, which is OK): 100010110

## Post Information

• Total Posts in this topic: 2 posts
• Users browsing this forum: No registered users and 38 guests
• You cannot post new topics in this forum
• You cannot reply to topics in this forum
• You cannot edit your posts in this forum
• You cannot delete your posts in this forum
• You cannot post attachments in this forum