Wednesday 15 October 2014

How to convert binary to hexadecimal and back again (very simple shortcut)

Hexadecimal is a base 16 numbering system used in computers. If you are using the basic calculator on a Windows computer, you can swap the view to programmer from standard and easily translate between binary, decimal and hex. That being said however, there may be times when you are not allowed to use the calculator (like when taking a certification exam) so you should know this shortcut.

Step 1: Know the hexadecimal numbers through 15. 0 through 9 are all the same as decimal but 10-15 are not, since you cannot have a two digit number in a single bit. 

10 - A
11 - B
12 - C
13 - D
14 - E
15 - F

So if someone says 10 in decimal, they would mean A in hex. If someone said 10 in binary, they would mean 2 since 2^0 is 0 plus 2^1 is 2, equaling 2. See my previous tutorial for decimal/binary help: Decimal to Binary and Back

Ok so now that you might be saying to yourself, "I thought we would go to 16 since it's a base 16 system". Remember that we are starting at 0, which means 0 is the 1st number, 1 is the 2nd, 2 is the 3rd and so on with 15 being the 16th number. So 0-15 is 16 numbers.

Step 2: Acquire your binary or hex number that you would like to convert. Lets pick a random binary number, say 11010001. If you refer to the tutorial above, you would know that the binary number is 209 in decimal but we want to go to hex. Break the binary number into groups of 4 starting from the right side to the left. The groups would be:

1101 0001

Step 3: Match the number of that group to the corresponding hex value. The group on the left hand side, 1101, is equal to 13 in decimal. If you refer to the hex chart above, you will see that 13 in decimal equals "D". So that group of 4 is D. The second group is 0001 or just plain 1. 1 is the same across each three numbering systems so that group would simply be 1.

Combine those two values and you have D1 which is the correct answer.

Now lets try a harder number. 1101011010001

Let's break it into groups of four again, starting from the right hand side and moving left. So we have (1) (1010) (1101) (0001). Notice that the final group only has 1 number in it. That's ok. Often times you will not have a binary number that is perfectly divisible by 4. Not to worry, if you'd like you can simply add leading 0's so the group would be (0001). Either way, it is 1.

So, the first group on the left, (1), is simply 1. The second group, (1010), is 10 (decimal) or A in hex. The third group, (1101), is 13 (decimal) or D in hex. The final group, (0001), is simply 1. Combine all those values together and you get 1AD1.

It is exceedingly simple and only requires the memorization of that chart above, 10-15 or A-F. The long way to convert a number to hex, say 272 (decimal) is more of a challenge. Like binary, hex is translated using powers. Instead of a base 2 for binary, it's a base 16. For the binary tutorial, we talked about the place values being 2^0, 2^1, 2^2, 2^3 and so on. For hex, simply substitute 2 for 16 so it would be 16^0, 16^1, 16^2, 16^3 and so on. 

So the first group, 16^0, would be 1 since anything to the 0 power is 1. The second group, 16^1, would be 16. The third group, 16^2, would be 256 since 16 x 16 is 256. The fourth group, 16^3, would be 4096 since 16 x 16 x 16 is 4096. As you can see the numbers get very large very quickly, which helps out a great deal in usable address space (for IPv6).

So using the number above, 272 in decimal, would be 110 in hex. We know this because 110 in hex is:



So in essence, you would be adding 1 group of 256 with a group of 16 and no groups of 1 which equals 272.

Say you have 4100 (decimal) and want to convert it to hex the long way. First, you can write out your place values, 16^0, 16^1, 16^2, 16^3. 16^4 would be 65,536 and since you do not have a group of 65,536 in 4100, there is no need to go to that place value. 16^3 is 4096 and you do have 1 group of 4096 in 4100 so you will use that place value. So put a 1 in the 4096 place value, since you have a group of 4096 in 4100. Then you have 4 left over. 

Go to your next place value of 16^2 or 256. Do you have a group of 256 in 4? No, so put a 0 in that place value. Next go to 16^1 or 16. Do you have a group of 16 in 4? No, so put a 0 in that place value. Next is 16^0 or 1. Do you have a group of 1 in 4? Yes, you have 4 so put 4. Now you are done. Combine the values you got and the hex number is 1004.

No comments:

Post a Comment

Wildern Pupils if you log onto your school email account you can leave a comment via that ID.