Menu

Memory & Storage

Denary to Hexadecimal Conversion

Contents

  1. Denary to Hexadecimal by Division
  2. Checking a Denary to Hexadecimal Conversion
  3. Denary to Hexadecimal using Binary

To turn a denary whole number from 0 to 255 into hexadecimal, divide it by 16. The whole-number part of the answer and the remainder give you the two hex digits, one each.

Quotient first, remainder second

The whole-number part of the division is called the quotient. It counts how many whole 16s fit into the number, so it becomes the left hex digit. The remainder is what is left over; it counts the 1s, so it becomes the right digit.

For example, 150 ÷ 16 = 9 remainder 6. The quotient 9 is the left digit and the remainder 6 is the right digit, so 150 is 96 in hexadecimal. The place values confirm it: 9 × 16 = 144, and 144 + 6 = 150.

Dividing by 16 always leaves a remainder from 0 to 15, so the remainder fits in one hex digit. For a number up to 255, the quotient is at most 15, so it fits in one digit as well. Any quotient or remainder from 10 to 15 must be written as its letter (10 = A up to 15 = F). For example, 43 ÷ 16 = 2 remainder 11, so 43 is 2B. Writing it as 211 would be wrong, because each character must be a single hex digit from 0 to F and the answer must be exactly two characters long.

Numbers below 16

If the denary number is smaller than 16, no whole 16 fits into it, so the quotient is 0. The answer still needs two digits, so write the 0 first. For example, 5 ÷ 16 = 0 remainder 5, giving 05, not 5. The left digit is worth 0 × 16 = 0 and the right digit is worth 5, which together make 5.

Always put the quotient before the remainder. Swapping them changes the value completely: 96 means 9 × 16 + 6 = 150, while 69 means 6 × 16 + 9 = 105.

You can always check a denary to hexadecimal conversion by turning the answer back into denary. A two-digit hex number has place values of 16 and 1, so multiply the left digit by 16, add the right digit, and see whether the result matches the number you started with.

For example, if your answer for 173 is AD, change A back to 10, work out 10 × 16 = 160, then add D, which is 13: 160 + 13 = 173. This matches the original, so the conversion is correct.

Three quick checks

  • The answer must be exactly two characters, and every character must be a single hex digit from 0 to F. An answer such as 2F4 cannot be right because it has three characters.
  • Work out the value from the two digits and check it lies between 0 and 255, the range covered by 00 to FF. A value outside that range means a character is not a valid hex digit.
  • The left digit counts 16s, not 10s. In 4A the 4 is worth 4 × 16 = 64, and the A is worth 10, giving 74 — not 410 by joining the digits, and not 50 by using denary place values.

Letters must be changed to 10 to 15 before you multiply. If you treat A as 1, or forget that it stands for 10, the check will not work. Never read a hex answer as though it were a denary number, and never assume a letter has no fixed value: A always means 10, B always means 11, and so on up to F for 15.

Hex answerWorking backwardsDenary value
5B5 × 16 + 1191
0E0 × 16 + 1414

A leading zero is still a character, so 0E is a valid two-digit answer and checks correctly to 14. Dropping it would break the rule that the answer is always two hex digits.

Going through binary is a reliable way to turn a denary number into hexadecimal, because a byte splits neatly into two halves and each half is exactly one hex digit. It also gives you a way to check an answer you found by dividing by 16.

Write the number as 8 bits

First convert the denary number into 8-bit binary using the place values 128, 64, 32, 16, 8, 4, 2 and 1. Take each place value that fits into what is left over, then pad with leading zeros until there are exactly eight bits.

For 197: 128 fits, leaving 69. The 64 fits too, leaving 5. The 32, 16 and 8 do not fit, but the 4 does, leaving 1, and then the 1 fits. The unused places hold 0, giving 11000101.

Split into nibbles and convert each one

Break the eight bits into two nibbles of four bits: 1100 and 0101. Each hex digit represents exactly one nibble, so the left nibble becomes the first hex digit and the right nibble becomes the second. Convert a nibble using its own place values of 8, 4, 2 and 1, and write any total from 10 to 15 as its letter.

NibblePlace values usedDenary totalHex digit
11008 + 412C
01014 + 155

Reading the two digits in order gives C5, so 197 is C5 in hexadecimal. You can check it: C is 12, and 12 × 16 + 5 = 197.

A nibble can only ever total 0 to 15, which is why this method can never produce a digit beyond F.

Numbers below 16

A denary number under 16 fits inside four bits, so its left nibble is 0000 and the answer begins with 0. The number 9 is 00001001, which splits into 0000 and 1001: 0 and 9, giving 09 rather than 9. Keeping the zero means the answer is always two hex digits long, matching one byte.

Also useful