NOGDUS Archives - Manual Number Base Conversions
Originally written for NOGDUS in 2008. Edited and formatted for ToV in 2014.
As a programmer, numbers should not intimidate you. I find that many people (myself included) have trouble understanding different number systems such as hexadecimal and binary. I recently had to deal with these two bases a LOT and learned a few tricks that I'm going to share with you.
You can easily convert binary to hex as long as you can count. Let us start simple. The letter A in ASCII has a decimal value of 65. Let us see what that looks like to the computer eg. in binary.
Well thats confusing isn't it? Not really if you understand how to read it. I'm going to display that vertical to make explaining this easier.
0 - bit # 7 | 128 off 1 - bit # 6 | 64 on 64 + 0 - bit # 5 | 32 off 0 - bit # 4 | 16 off 0 - bit # 3 | 8 off 0 - bit # 2 | 4 off 0 - bit # 1 | 2 off 1 - bit # 0 | 1 on 1 = 65
Okay? Got it? It is just that simple. Big numbers scare the hell out of a LOT of people. You're about to see some VERY big numbers. Don't run away! Now, counting keeps going the more bits your have (that was just 1 byte) What if you have a 32-bit number? Well you have to keep on adding up those numbers!
Let us take the decimal value 4294836225 for instance. In binary it looks like this:
11111111 11111110 00000000 00000001 Byte 3 Byte 2 Byte 1 Byte 0
But what does it mean and how does that mean that BIG number? I'm going to show you how! And I promise this is the last vertical binary listing I'll do.
Byte 3 1 - bit # 7 | 2147483648 on 2147483648 + 1 - bit # 6 | 1073741824 on 1073741824 + 1 - bit # 5 | 536870912 on 536870912 + 1 - bit # 4 | 268435456 on 268435456 + 1 - bit # 3 | 134217728 on 134217728 + 1 - bit # 2 | 67108864 on 67108864 + 1 - bit # 1 | 33554432 on 33554432 + 1 - bit # 0 | 16777216 on 16777216 + Byte 2 1 - bit # 7 | 8388608 on 8388608 + 1 - bit # 6 | 4194304 on 4194304 + 1 - bit # 5 | 2097152 on 2097152 + 1 - bit # 4 | 1048576 on 1048576 + 1 - bit # 3 | 524288 on 524288 + 1 - bit # 2 | 262144 on 262144 + 1 - bit # 1 | 131072 on 131072 + 0 - bit # 0 | 65536 off Byte 1 0 - bit # 7 | 32768 off 0 - bit # 6 | 16384 off 0 - bit # 5 | 8192 off 0 - bit # 4 | 4096 off 0 - bit # 3 | 2048 off 0 - bit # 2 | 1024 off 0 - bit # 1 | 512 off 0 - bit # 0 | 256 off Byte 0 0 - bit # 7 | 128 off 0 - bit # 6 | 64 off 0 - bit # 5 | 32 off 0 - bit # 4 | 16 off 0 - bit # 3 | 8 off 0 - bit # 2 | 4 off 0 - bit # 1 | 2 off 1 - bit # 0 | 1 on 1 = 4294836225
As you can see above, each bit (counting from the right) is exactly double the previous bit. Okay, so now you know a bit about binary numbers.What about the cryptic hexadecimal numbers that scare away so many? Yeah thats easy too. I'm sure that you have seen hexadecimal numbers somewhere in your programming life. If not, if you are running linux, open a terminal and type in xxd -u /boot/vmlinux > ~/vmlinux.hex and open that up in a text editor. I like SciTE personally. Okay, so yeah..thats hex and quite confusing if you're uninformed.
Hexadecimal, or Hex for short, is a base 16 counting system.
Decimal: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Hexadecimal: 0 1 2 3 4 5 6 7 8 9 A B C D E F
Let's look at the letter A in decimal again: 65
Now, in hexadecimal what would that be? Well there is an easy way to find out! Remember the binary for 65? Here it is again incase you forgot.
Cut that in half creating two "nibbles" of 4 bits each:
left (low) nibble right (high) nibble 0100 0001
Forget about the high nibble for now. Now you only have 4 bits to worry about.
That in itself is what? ... (look at the first vertical byte list if you forgot)
Got it yet? I did. .... 4h!
Now what about the high nibble?
Now put them together, 41h ... that is the hexadecimal equivalent of 65 in decimal! So, when ever you need to convert a decimal number to hex, its easiest to first get the binary of it, then bust that into nibbles. :) As a final example, lets look at a stream of binary bits of no origin than my head.
101010101100010111100010101010010101001010100101
Yikes... Okay.. We can do this...Easy.
First, just break that scary string of digits into nibbles:
1010 1010 1100 0101 1110 0010 1010 1001 0101 0010 1010 0101
Now let us convert each nibble into hexadecimal
That wasn't hard. Okay, that is it for today. Want to know more? Ask me.