How to Convert Binary to Hex and Octal
Group bits in fours for hex and threes for octal. A nibble-to-hex table, worked examples both ways, and why programmers write binary as hex at all.
Why Hex and Octal Are Not Another Language
Hexadecimal and octal are shorthand for binary, nothing more. Every four binary digits collapse into one hex digit, and every three collapse into one octal digit. That is the entire trick. If you can count from zero to fifteen, you can convert binary to hex. If you can count from zero to seven, you can convert binary to octal. The mapping is fixed, it never changes, and it is the reason programmers write 0xFF instead of 11111111. The binary to hex conversion is not arithmetic you perform; it is a table you memorize until it becomes as immediate as reading a clock.
Binary to Hex: Group in Fours
To convert binary to hexadecimal, start at the rightmost bit and split the binary string into groups of four. If the leftmost group has fewer than four bits, pad it with leading zeros. Then replace each group with its hex equivalent. The mapping is direct because 2 to the fourth power is 16. A nibble, which is a group of four bits, holds one hex digit. So 1010 becomes A, 1100 becomes C, and 1111 becomes F.
Work an example. Take the binary number 1101011011. Starting from the right, split it as 11 0101 1011. Pad the leftmost group to 0011. Now convert each nibble: 0011 is 3, 0101 is 5, 1011 is B. The hex result is 0x35B. Notice the leading zero in the leftmost group changes nothing; it only exists to make the group a full nibble. If you drop it, the answer is still 35B, but keeping it helps you see the grouping clearly when you are first learning.
The practical rule is this: never convert through decimal. Going binary to decimal to hex works but costs you time and invites errors. Go straight from nibble to hex digit. After a dozen conversions you will stop counting powers of two and simply recognize that 1010 is A because it is ten. That recognition is the skill that makes hex to binary and binary to hex instant.
Hex to Binary: The Reverse Mapping
Converting hex to binary is the exact reverse of the process above. Take each hex digit and write its four-bit binary equivalent. The table is symmetric: 0 is 0000, 1 is 0001, 2 is 0010, and so on up to F which is 1111. Write the groups in order, left to right, and concatenate them. Do not drop leading zeros from the first group, because the hex digit F represents four bits and those zeros are part of the value's bit pattern.
For example, convert 0x2A to binary. The 2 becomes 0010, and the A becomes 1010. Concatenate them to get 00101010. The leading zeros in the 2 group matter here: if you wrote 10 instead of 0010, you would get 101010, which is 42 as well, but only because the leading zeros carry no value. In a context where you need an 8-bit field, 0x2A must be written as 00101010, not 101010, because the register expects eight bits. Always pad the full width you are working with.
The most common failure when going hex to binary is sign extension. If you have a signed 8-bit value 0xFF and you widen it to 16 bits, you must copy the most significant bit into the new high bits. 0xFF as a signed byte is -1. Widened without sign extension, it becomes 0x00FF, which is 255, not -1. This is not a hex problem; it is a bit-pattern interpretation problem, but it bites people when they convert hex to binary and then back again.
Binary to Octal: Group in Threes
To convert binary to octal, start at the rightmost bit and split the binary string into groups of three. Pad the leftmost group with leading zeros if needed. The reason is that 2 to the third power is 8, so each octal digit represents three bits. The mapping is 000 is 0, 001 is 1, 010 is 2, 011 is 3, 100 is 4, 101 is 5, 110 is 6, and 111 is 7.
Take the binary number 1101011011 again. Split it from the right as 1 101 011 011. Pad the leftmost group to 001. Now convert each group: 001 is 1, 101 is 5, 011 is 3, 011 is 3. The octal result is 1533. Notice this is the same binary number that became 0x35B in hex. Octal is less common than binary to hex today, but you will still meet it if you read old documentation or work with certain embedded systems. If you can do one, you can do the other, and the only trap is using the wrong group size. Mixing them up, grouping in fours when you mean threes, produces nonsense, so check the base before you start.
The Nibble Table You Need to Know
The table below is the one you should memorize before anything else. It lists every four-bit binary value from 0000 to 1111 alongside its hex digit and its decimal equivalent. There are only sixteen rows. If you learn this table, you can convert binary to hex and hex to binary without touching a calculator. You will also understand why 0xA is ten and why 0xF is fifteen, which is the foundation of every hex conversion table you will ever use.
Notice the pattern in the high bit. The last eight rows, 1000 through 1111, have a most significant bit of one and map to 8 through F. That single bit is what separates the lower half of the nibble from the upper half, and it is the bit that becomes the sign bit when you interpret the byte as signed.
| Binary | Hex | Decimal |
|---|---|---|
| 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 |
Where You Meet Hex in Practice
RGB colors are the first place. A color like #FF8800 is a compact way to write three bytes: FF for red, 88 for green, 00 for blue. Without hex, you would have to write 255, 136, 0 in decimal, which is three numbers instead of one compact six-digit token.
Memory addresses are the second place. When a debugger shows you an address like 0x7FFF_FFF0, it is giving you a bit pattern in a form that is easy to read. Each hex digit is one nibble, so you can see the underlying binary structure without counting long runs of ones and zeros. This matters when you are aligning data or checking whether a pointer is word-aligned, which usually means the low bits are zero.
Byte values are the third. A byte is eight bits, which is two nibbles, which is two hex digits. The benefit is that every byte becomes two characters, and every pair of hex digits maps directly to a bit pattern you can decode. For binary to decimal and decimal to binary conversions, you do not need hex at all, but for anything that touches memory or raw data, hex is the language.
One warning: do not assume every system uses the same byte order. A multi-byte integer stored in memory can be little-endian, with the least significant byte first, or big-endian, with the most significant byte first. Hex does not tell you which one you are looking at. If you read a 32-bit value like 0x12345678 from a file and the bytes on disk are 78 56 34 12, you are on a little-endian machine. The order is the only thing that changes, and getting it wrong is a classic failure mode in binary work.
What to Do When You Cannot Convert
When the normal route of doing it in your head fails, usually because you are tired or the number is long, stop guessing and write it down. Take the binary string and literally put spaces between every four bits from the right. If you still cannot recall that 1010 is A, you have not memorized the table yet, so go back to the table above and drill it for five minutes. That is the whole cost.
If you are in a debugger or a REPL, do not hand-convert at all. Use the built-in functions. These functions exist because hand conversion is error-prone, not because it is hard. Use them when the answer matters and you are not being tested on the method.
The one thing that will fail you is assuming the conversion you did in your head is right without checking. If you converted binary to hex by grouping, check it by converting the hex back to binary and seeing if you get the original string. This verification takes ten seconds and catches nearly every mistake.
Practice on Real Bytes
Take any random byte, say 10110110, and convert it to hex by hand. Do that for ten different bytes, then reverse it, converting ten hex pairs back to binary. If you cannot do one byte in under five seconds, you have not internalized the nibble table yet. Do not move on to octal until you can.
Once the table is automatic, the rest is just grouping. The skill compounds because every hex digit you read instantly tells you the four-bit pattern, and every four-bit pattern tells you the hex digit. That bidirectional mapping is the entire subject. Binary to hex is not a calculation you perform; it is a reading skill you build.
Common Questions
Why do we group binary in fours for hex but threes for octal?
Because 2 to the fourth power is 16. Four bits can represent 16 distinct values, one hex digit. Three bits represent 8 values, one octal digit. The group size always matches the power of two that equals the target base.
What is the fastest way to convert binary to hex by hand?
Memorize the sixteen nibble-to-hex pairs from 0000 to 1111. Do not convert through decimal; the whole point of hex is that it is a direct shorthand for binary.
Is 0xFF always 255?
Only if you treat it as unsigned. The interpretation depends on whether you read the most significant bit as a value bit or a sign bit. The hex digits do not tell you which one you mean.
What is the most common mistake when converting hex to binary?
Dropping leading zeros from the first hex digit. 0x2A must become 00101010, not 101010, if you are working with an 8-bit field. The second most common mistake is sign-extending when you should not, or failing to sign-extend when you must.