Converting Binary to Hexadecimal
Every now and then itâ€™s nice to take break from tutorials, and look at something a little geekier. Weâ€™ve all seen binary code, and most people know itâ€™s made up of two characters, zero and one. Most people also understand that binary code can be converted into decimal by taking the binary number from the right-hand-side of the sequence and applying it to another sequence of numbers â€œtwo to the power ofâ€. For example an eight digit binary code could look like this â€“ â€œ10010001â€. This includes one â€œ1â€, one â€œ16â€, and one â€œ128â€, making a grand total of 145. Thatâ€™s fairly simple. It gets more complex when you add more numbers but the principal is the same.
Now â€“ if you do a lot of HTML work, you will have seen codes that represent colors. These codes include letters, and are in hexadecimal. The letters are actually numbers but because the characters we commonly use are in base 10, i.e. the digits 0 through to 9, we have to use letters to represent the numbers 10,11,12,13,14 and 15. This is because hexadecimal is base 16, and has 16 digits including zero. We donâ€™t use two characters in a hexadecimal number until we get to the number 16, which is actually â€œ10â€. This represents one sixteen. The hexadecimal number â€œ18â€ is actually sixteen plus 8, which is 24. The hex code â€œ1Fâ€ represents 31 in decimal because we have one sixteen plus 15. Get the idea?
Okay, so we can mostly work out what a hex number in decimal is when we only have two characters. One digit represents how many â€œ16â€s we have, and the other is simply counting from zero to fifteen. So the highest number we have is â€œFFâ€, which is fifteen times sixteen, plus fifteen. The answer is 255. Already you can see the benefits of hex, because in binary that number would require eight characters, and weâ€™ve done it in two.
So how do we solve bigger numbers into decimal easier? The answer is to split each hex character into its binary equivalent. So the hex number 8FA4 becomes in binary 1000.1111.1011.0100. Here we can see each number that makes up the ultimate answer. Starting from the left we have a 4, a 16, a 32, a 128 etcâ€¦ all the way up to the last character, which is a 32,768. Add all the numbers represented by ones together and we have 36772. So our hex code of 8FA4 is actually 36772 in decimal. Four characters to represent something that is sixteen characters in binary is very impressive indeed.
If you remember back your hex codes in HTML that represent colors, youâ€™ll notice that you have hex codes of 6 characters. The first character represents 16â€™s, the second 16â€™s to the power 2, the third is sixteen to the power 3 and so on. If you work it out, the largest number you can have with six hex digits is 16,777,215. Nearly seventeen million colors represented by only six characters.Advertisement