Lesson 1: Converting between hex, binary, and decimal.
The first thing you must know to
program in a low level language is the common number bases.
Decides knowing the human base 10 decimal system, you also need
to know hexadecamal an binary. At first I figured I could get by
without this... Boy was I wrong. So, Lets start with hex.
Hex is base 16. The range for a hex digit
is 0-9 and then A-F. Hex is usually expressed in two characters
at a time. Here are some examples of hex numbers: $3C, $AF, $0F,
B4h. A leading dollar sign ($) or a trailing lowercase 'h'
signifies that the number is a hex digit.
So how do you go from hex to our number system? Ill show you.
Lets convert $3C. First you take the first digit (3) and multiply
it by 16. 3*16=48. Then you take the second number (C), which is
12, and add it to the number before, 48. That gives us an anwser
of 60. Therefore $3C = 60. Just for refrence, here are the hex
letters and their decimal equivilants:
A = 10 | B = 11 | C =1 2 |
D = 13 | E = 14 | F = 15 |
Now lets move on to binary. Binary is base 2, meaning the
range of values is 0-1, which is called a bit. 8 bits make up a
byte, which is the standard unit when dealing in binary.
Converting to decimal from binary sounds harder than it actually
is, so bare with me.
Binary is written with a percent sign (%) leading or a lowercase
'b' trailing. Lets take the following number, %10010101 and
convert it to decimal. Each bit has a specific value: Using a
grid, Conversion is really easy.
|
Take this grid. | ||||||||||||||||||||||||
|
Put the number below in the grid. | ||||||||||||||||||||||||
|
And carry down where there are 1's And add them up: 128+16+4+1=149. |
Range Of Values
Lets look at the range of values. For a byte, the highest is %11111111. Lets throw it in the table.
|
128+64+32+16+8+4+2+1=255 |
So, the range of values for a single byte is 0-255, leaving 256 different values(2^8=256).
Now lets look at a hex digit.
Well, the highest hex digit is $FF. Lets do the math.
F=15, so 15*16=240. 240+15=255
Hmmm. The range of values of a byte and a hex digit is the same. So, %11111111=$FF.
So, hex and binary are closly intertwined. This makes it easy to sonvert between them.
Converting between hex and binary
Technically, each hex chracter is called a nibble which is 4 bits, or half a byte. You will notice the range of values for a nibble is 0-F in hex, 0000-1111 in binary, and 0-15 in decimal.
So lets take %1011010 and convert it to hex.
First, take the back 4 bits: %1010. This converts to decimal as 10. 10 in hex is A.
Now lets take the rest: %101. You can imagine in a zero in front if you please, so lets convert %0101. That equals 5, in hex and decimal. So, take the two digits, and you come up with $5A.
Lets just check that. 5*16=80, 80+10=90.
|
64+16+8+2=90. |
So, in conclusion, each hex digit, 0-F, is a nibble, and two nibbles make up a byte.
Here is a quick lookup chart for nibbles:
|
With this chart, you can quickly go between hex and
binary. Lets just quickly do one. $E8 = %11101000 Ah, another wont hurt. %10011011 = $9B. |
You now know the basics of hex and binary, and how to convert between bases. If you don't get it now, don't worry. It will come eventually.