Associated Topics || Dr. Math Home || Search Dr. Math

### Converting Binary and Decimal to Hexadecimal and Back

```
Date: 08/12/2001 at 22:56:40
From: robert coleman
Subject: Bin to hex conversions also decimal to hex

Convert binary numbers to hexadecimal, and reverse. Also convert
```

```
Date: 08/13/2001 at 03:33:30
From: Doctor Jeremiah
Subject: Re: Bin to hex conversions also decimal to hex

Hi Robert,

It would helps if you explained your current level of understanding
and described what kind of answer you were looking for. Since I don't
have this information I can only give you a basic overview of the
subject. I encourage you to write back with a more specific question.

As you probably know, binary numbers have digits just like decimal
numbers with one difference: the decimal digits go from 0 to 9, but
the hexadecimal digits can only go from 0 to 1.

So as with decimal numbers, when you get to 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 ...

Hexadecimal numbers are exactly 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 some
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 right.

So hex numbers go: 0, 1 ... 9, A ... F, 10, 11 ...

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

You could put in commas every four places (starting on the left):

binary:  100,0110,1101,1110,1010,0010,0001

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

Do you 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 0101 1101 1110 1010 0010 0001

So converting between binary and hex is easy because it has been
defined in such a way that is easy.

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. For example:

7431 = 7*10^3 + 4*10^2 + 3*10^1 + 1*10^0

where ^ means "with an exponent of"

And binary is the same way:

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:

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

converting to decimal is the easy direction. To convert decimal to
binary you do this:

decimal: 12345

Step 1:  Divide the decimal number by 2. The remainder is the
rightmost binary digit.

Step 2:  Then truncate the result (take just the integer part and
throw away the remainder).

Step 3:  Divide the integer decimal number from Step 2 by 2.
The remainder is the binary digit just to the left
of the ones you have found up until now.

step 4:  If the integer part is zero, you are done. If not,
Go to Step 2.

When you actually do it it looks like this:

decimal: 12345
Step 1:  12345/2=6172 remainder 1  ==>              1
Step 2:  new value is 6172
Step 3:  6172/2=3086 remainder 0   ==>             01
Step 4:  3086 > 0 so go to step 2
Step 2:  new value is 3086
Step 3:  3086/2=1543 remainder 0   ==>            001
Step 4:  1543 > 0 so go to step 2
Step 2:  new value is 1543
Step 3:  1543/2=771 remainder 1    ==>           1001
Step 4:  771 > 0 so go to step 2
Step 2:  new value is 771
Step 3:  771/2=385 remainder 1     ==>          11001
Step 4:  385 > 0 so go to step 2
Step 2:  new value is 385
Step 3:  385/2=192 remainder 1     ==>         111001
Step 4:  192 > 0 so go to step 2
Step 2:  new value is 192
Step 3:  192/2=96 remainder 0      ==>        0111001
Step 4:  96 > 0 so go to step 2
Step 2:  new value is 96
Step 3:  96/2=48 remainder 0       ==>       00111001
Step 4:  48 > 0 so go to step 2
Step 2:  new value is 48
Step 3:  48/2=24 remainder 0       ==>      000111001
Step 4:  24 > 0 so go to step 2
Step 2:  new value is 24
Step 3:  24/2=12 remainder 0       ==>     0000111001
Step 4:  12 > 0 so go to step 2
Step 2:  new value is 12
Step 3:  12/2=6 remainder 0        ==>    00000111001
Step 4:  6 > 0 so go to step 2
Step 2:  new value is 6
Step 3:  6/2=3 remainder 0         ==>   000000111001
Step 4:  3 > 0 so go to step 2
Step 2:  new value is 3
Step 3:  3/2=1 remainder 1         ==>  1000000111001
Step 4:  1 > 0 so go to step 2
Step 2:  new value is 1
Step 3:  1/2=0 remainder 1         ==> 11000000111001
Step 4:  0 = 0 so quit

So decimal 12345 is actually binary 11000000111001

Hex is done the same way, except instead of dividing by 2 you divide
by 16.

That's basically how conversions between bases works. If this doesn't

- Doctor Jeremiah, The Math Forum
http://mathforum.org/dr.math/
```
Associated Topics:
High School Number Theory

Search the Dr. Math Library:

 Find items containing (put spaces between keywords):   Click only once for faster results: [ Choose "whole words" when searching for a word like age.] all keywords, in any order at least one, that exact phrase parts of words whole words

Submit your own question to Dr. Math
Math Forum Home || Math Library || Quick Reference || Math Forum Search