Introduction to Binary / Decimal / Hexadecimal Conversion
Check out the notes in the Summary of Classes Section
Converting Binary into Decimal
To convert from binary to decimal, just multiply each bit by the value of its position, and add the results for all positions. The right-most position is worth one. When dealing with binary numbers, the value of each position increases by a factor of two as we move left, so the next position to the left is worth two, the next position is worth four, the next is worth eight, etc. Again, for fans of scientific notation, the base is two, and the exponents start at zero for the right-most position, and increment by one for each position as we move left. Therefore, converting the binary number 01101 to decimal (starting from the right and moving leftwards) gives [1 x 1] + [0 x 2] + [1 x 4] + [1 x 8] + [0 x 16], or 13. You can start at the left and move right if you find that more convenient, just make sure you do the math right.
Alternative method of converting decimal into binary
Going from decimal to binary is easy. Just use this algorithm:
Divide the decimal number (which mathematicians would call the "dividend") by 2 (the "divisor"). Write down the result (the "quotient") and what is left over (the "remainder"). Using the quotient as the new dividend, repeat until the dividend is zero. Now, read the remainders in reverse order, and you have the binary equivalent of the original decimal number.
For example, suppose we want to convert the decimal number 123 to binary ...
123 / 2 -> Q = 61, R = 1
61 / 2 -> Q = 30, R = 1
30 / 2 -> Q = 15, R = 0
15 / 2 -> Q = 7, R = 1
7 / 2 -> Q = 3, R = 1
3 / 2 -> Q = 1, R = 1
1 / 2 -> Q = 0, R = 1
Then simply read the remainders from the bottom to the top: 1111011.
The binary equivalent of decimal 123 is 1111011. We can check our work by using the method from the previous problem, which (working from right to left) gives [1 x 1] + [1 x 2] + [0 x 4] + [1 x 8] + [1 x 16] + [1 x 32] + [1 x 64], or 123. If we want to represent the answer as an eight-bit binary number, just add a zero on the left for 01111011.
As a slide
Divide the decimal number (which mathematicians would call the "dividend") by 2 (the "divisor"). Write down the result (the "quotient") and what is left over (the "remainder"). Using the quotient as the new dividend, repeat until the dividend is zero. Now, read the remainders in reverse order, and you have the binary equivalent of the original decimal number.
For example, suppose we want to convert the decimal number 123 to binary ...
123 / 2 -> Q = 61, R = 1
61 / 2 -> Q = 30, R = 1
30 / 2 -> Q = 15, R = 0
15 / 2 -> Q = 7, R = 1
7 / 2 -> Q = 3, R = 1
3 / 2 -> Q = 1, R = 1
1 / 2 -> Q = 0, R = 1
Then simply read the remainders from the bottom to the top: 1111011.
The binary equivalent of decimal 123 is 1111011. We can check our work by using the method from the previous problem, which (working from right to left) gives [1 x 1] + [1 x 2] + [0 x 4] + [1 x 8] + [1 x 16] + [1 x 32] + [1 x 64], or 123. If we want to represent the answer as an eight-bit binary number, just add a zero on the left for 01111011.
As a slide
Hexadecimal
- As we only know the digits 0-9, we need more digits to show 10 - 15
Hence: A = 10, B = 11, C = 12, D = 13, E = 14, F = 15
Because each hex digit is worth four binary bits, we can use hex as shorthand for binary numbers.
To convert a binary number to its hex representation, first group the bits into "nibbles" (a "nibble" is four bits, or half a byte), starting from the right. For example, given the binary number 10110110011, the right-most nibble is 0011. The next nibble to the left is 1011. The left-most nibble is 0101 (note the addition of a leading zero to get four bits). Now, convert each nibble to its hex equivalent, using the following table:
Using the nibbles 0101 1011 0011, we obtain the values 5 B 3. Finally, concatenate (smash together) the hex digits in a suitable manner, such as 5B3, 05-B3, or whatever representation is handy, typically pairs of hex digits separated by dashes. This is done because a pair of hex digits is two nibbles, or one eight-bit byte.
Binary Hex Dec
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 A 10
1011 B 11
1100 C 12
1101 D 13
1110 E 14
1111 F 15
To go from hex to binary, just reverse the process from above. Using the table, convert each hex digit into its binary (nibble) equivalent, and then concatenate the nibbles. For example, to convert the hex value A2F to binary, convert the hex digits A, 2 and F to binary, which gives 1010 0010 1111, and smack them together (in order) to get 101000101111.
To convert a binary number to its hex representation, first group the bits into "nibbles" (a "nibble" is four bits, or half a byte), starting from the right. For example, given the binary number 10110110011, the right-most nibble is 0011. The next nibble to the left is 1011. The left-most nibble is 0101 (note the addition of a leading zero to get four bits). Now, convert each nibble to its hex equivalent, using the following table:
Using the nibbles 0101 1011 0011, we obtain the values 5 B 3. Finally, concatenate (smash together) the hex digits in a suitable manner, such as 5B3, 05-B3, or whatever representation is handy, typically pairs of hex digits separated by dashes. This is done because a pair of hex digits is two nibbles, or one eight-bit byte.
Binary Hex Dec
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 A 10
1011 B 11
1100 C 12
1101 D 13
1110 E 14
1111 F 15
To go from hex to binary, just reverse the process from above. Using the table, convert each hex digit into its binary (nibble) equivalent, and then concatenate the nibbles. For example, to convert the hex value A2F to binary, convert the hex digits A, 2 and F to binary, which gives 1010 0010 1111, and smack them together (in order) to get 101000101111.
Hexadecimal into Decimal - Method 1
165 | 164 | 163 | 162 | 161 | 160 |
1 048 576 | 65 536 | 4096 | 256 | 16 | 1 |
Going from hex to decimal is similar to going from binary to decimal. Just convert each hex digit to its decimal equivalent, multiply by the value of its position, and add the results for all positions. As with binary and decimal, the right-most position is worth one. When dealing with hex numbers, the value of each position increases by a factor of sixteen as we move left, so the next position to the left is worth sixteen, the next position is worth 256 (decimal), the next is worth 4,096, etc. Again, for fans of scientific notation, the base is sixteen, and the exponents start at zero for the right-most position, and increment by one for each position as we move left. Therefore, converting the hex number 2AB to decimal (starting from the right and moving leftwards) gives [11 x 1] + [10 x 16] + [2 x 256], or 683. As with binary, you can start at the left and move to the right, just make sure that you don't scramble the values while multiplying and adding positions.
Hexdecimal and Decimal Conversions - Method 2
Decimal to Hexadecimal ConversionsThe easiest way to get from decimal to hexadecimal and back is to go through binary. Take the example we used earlier in which we converted 176 decimal to binary:
176 = 10110000
Given that a single hex character represents four binary bits, all we need to do is to break the 8-bit string 10110000 into two 4-bit strings like this:
1011 0000
Now, simply match the 4-bit strings to their hex equivalent:
1011 = B
0000 = 0
The answer is simply 10110000 = 0xB0.
The 0x in front of the answer is an expression that means “the following is in hex.” This is needed because if the hex value was 27, we could not distinguish it from 27 decimal.
Hexadecimal to Decimal ConversionsThe reverse of the procedure is easier than it seems, too. Given a hex value of 0xC4, all we need to do is to first convert to binary, and then to decimal.
To convert to binary, take the two hex characters and find their binary value:
C = 1100
0100 = 4
Now, make the two 4-bit strings into one 8-bit string:
11000100
Finally, add the bit values of the columns where you have a 1:
128 + 64 + 4 = 196
176 = 10110000
Given that a single hex character represents four binary bits, all we need to do is to break the 8-bit string 10110000 into two 4-bit strings like this:
1011 0000
Now, simply match the 4-bit strings to their hex equivalent:
1011 = B
0000 = 0
The answer is simply 10110000 = 0xB0.
The 0x in front of the answer is an expression that means “the following is in hex.” This is needed because if the hex value was 27, we could not distinguish it from 27 decimal.
Hexadecimal to Decimal ConversionsThe reverse of the procedure is easier than it seems, too. Given a hex value of 0xC4, all we need to do is to first convert to binary, and then to decimal.
To convert to binary, take the two hex characters and find their binary value:
C = 1100
0100 = 4
Now, make the two 4-bit strings into one 8-bit string:
11000100
Finally, add the bit values of the columns where you have a 1:
128 + 64 + 4 = 196