Converting Binary, Decimal and Hexadecimal

 

Binary numbers (base 2) and hexadecimal numbers (base 16) have digits just like decimal numbers (base 10) with two differences: decimal digits go from 0 to 9, and hexadecimal digits go from 0 (zero) to F while binary digits are 0 (zero) and 1.

 

So as with decimal numbers, when you get t o the largest number that is allowed in a digit you start on the next place value. In decimal, after you get to the largest digit value (9) you go back to 0, but you increment the digit in the place value to the left. Binary is the same; when you get to the largest digit value (1) you go back to 0 and increment the digit in the place value to the left.

 

So, binary numbers go (in order): 0, 1, 10, 11...

A binary 0 is zero or ‘off’ and a binary 1 is one or ‘on’. Binary numbers, as the name suggests, have a value in decimal of two because the binary number can either be on or off.  Binary is calculated from the place value that the 1 holds. An example of this is the binary number:

00000001. Starting from the left we record the position of the 1 as the decimal number one. The binary number 00000011 is equal to 1 plus 2 which is three in decimal. The binary number 00000111 is equal to 1 plus 2 plus 4 which seven in decimal.

 

The binary positions start from the right:

Binary:        1       1      1      1     1    1    1    1   

Decimal:   128    64    32    16    8    4    2    1

              

The binary bit values don’t stop at 128 they continue to infinity so, the next numbers in decimal are 256, 512, 1024, 2048, 4096, etc…..     

 

Hexadecimal numbers are almost the same except that there are 16 different digit values. Because each digit must be only one character, we need to use all the numbers (0 through 9) and then use the alphabetic characters (A through F).

 

So when a hexadecimal digit gets to 9, it doesn't go back to 0; instead it goes on to A. After it gets to F it goes back to 0 and increments the digit in the place value to the left.

 

So hex numbers go: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A (=Ten), B (=Eleven), C (=Twelve), D (=Thirteen), E (=Fourteen), F (=Fifteen)

 

 

 

 

Binary to hexadecimal is very easy because hexadecimal numbers are designed specifically so that each hex digit is exactly 4 bits (i.e. 16 different values). So if you had this binary number:

 

Binary:        100011011011110101000100001

 

Put in commas every four places (starting on the right and working your way to the left):

 

Binary:  100,0110,1101,1110,1010,0010,0001 START ON THIS SIDE

 

Then you could write the hex values immediately below:

 

Binary: 0100,0110,1101,1110,1010,0010,0001

Hex:        4       6       D      E       A      2       1

 

and the hex value would be 46DEA21.

 

How did we come up with this translation of digits?

 

   Binary  Decimal   Hex

    0000         0           0

    0001         1           1

    0010         2           2

    0011         3           3

    0100         4           4

    0101         5           5

    0110         6           6

    0111         7           7

    1000         8           8

    1001         9           9

    1010        10          A

    1011        11          B

    1100        12          C

    1101        13          D

    1110        14          E

    1111        15          F

 

Now you can see why I said that each hex digit was 4 bits.

 

To convert a hexadecimal number back to binary you generate 4 bits for each hex digit:

 

Hex:         4      6       D      E       A       2      1

Binary: 0100 0110 1101 1110 1010 0010 0001

 

Converting to and from decimal is harder because decimal is not based on an integral number of binary bits. We need to learn how to change between arbitrary bases. It all relies on how numbers work. 

Each place value in a number has a certain value. Take our decimal system for example:

Decimal

 

 

 

 

 

 

 

10^7

10^6

10^5

10^4

10^3

10^2

10^1

10^0

1

1

1

1

1

1

1

1

10000000

1000000

100000

10000

1000

100

10

1

 

7,431 = 7*10^3 + 4*10^2 + 3*10^1 + 1*10^0

 

Where ^ means "with an exponent of" or “raised to the power of”

 

And binary is the same way:

Binary

 

 

 

 

 

 

 

2^7

2^6

2^5

2^4

2^3

2^2

2^1

2^0

1

1

1

1

1

1

1

1

128

64

32

16

8

4

2

1

 

 

110101 = 1*2^5 + 1*2^4 + 0*2^3 + 1*2^2 + 0*2^1 + 1*2^0

 

So to convert binary into decimal all you need to do is add up those

values:

 

    1*2^5  =  32

 +  1*2^4 = 16

 +  0*2^3 =  0

 +  1*2^2 =  4

 +  0*2^1 =  0

 +  1*2^0 =  1

 -------      ----

 = 110101      = 53

 

That's how to convert binary into decimal.

 

Hex is done the same way:

Hexadecimal

 

 

 

 

 

 

 

16^7

16^6

16^5

16^4

16^3

16^2

16^1

16^0

1

1

1

1

1

1

1

1

268435456

16777216

1048576

65536

4096

256

16

1

 

F40A2 =  F*16^4 + 4*16^3 + 0*16^2 +  A*16^1 + 2*16^0

      = 15*16^4 + 4*16^3 + 0*16^2 + 10*16^1 + 2*16^0

 

  

(F) 15*16^4  =  983040     

 + (4)  4*16^3 =  16384

 + (0)  0*16^2 =         0

 + (A) 10*16^1=     160

 + (2)  2*16^0  =        2

 -------------      --------

 =   F40A2          = 999586