All posts
June 4, 20266 min read

Converting between binary, hex, and decimal

A number base is just the count of distinct digits a system uses before it rolls over to the next place. Decimal uses ten digits, 0 through 9. Binary uses two, 0 and 1. Hexadecimal uses sixteen: 0 through 9 then A through F, where A is ten and F is fifteen. The value of a number never changes when you switch bases. Only the way you write it down does. 255, 0xFF, and 0b11111111 are the same quantity in three costumes.

How the bases relate

Every base works by positional weighting. In decimal, each place is a power of ten. In binary, each place is a power of two. In hex, each place is a power of sixteen. Read a number right to left and multiply each digit by its place value.

Take the decimal number 156. In binary it is 10011100, which expands to:

128 + 0 + 0 + 16 + 8 + 4 + 0 + 0 = 156

In hex it is 9C, which expands to:

(9 x 16) + (12 x 1) = 144 + 12 = 156

Three notations, one value.

Why hex is convenient for bytes

This is the part worth internalizing. Sixteen is two to the fourth power, so one hex digit represents exactly four bits, often called a nibble. Two hex digits represent eight bits, which is one byte. That clean alignment is why hex shows up everywhere bytes do.

nibble  0000 = 0    nibble  1010 = A
nibble  1111 = F    byte 11111111 = FF

A byte ranges from 0 to 255, which is 00 to FF in hex. Reading raw memory, a binary file, or a network packet in hex lets you see byte boundaries at a glance. The same data in decimal hides those boundaries, and in binary it becomes an unreadable wall of ones and zeros.

You meet this daily without thinking about it:

  • Hex colors like #1E90FF are three bytes: red 1E (30), green 90 (144), blue FF (255). Each channel is one byte, so two hex digits each.
  • Memory addresses such as 0x7FFE0A3C are written in hex because the byte structure stays legible.
  • Unicode code points like U+1F600 and MAC addresses are hex for the same reason.

Converting by hand

To go from decimal to binary, subtract the largest power of two that fits, mark a 1, and repeat:

156 - 128 = 28   -> 1 in the 128 place
 28 - 16  = 12   -> 1 in the 16 place
 12 -  8  =  4   -> 1 in the 8 place
  4 -  4  =  0   -> 1 in the 4 place
result: 10011100

To go from binary to hex, split the bits into groups of four from the right and translate each group to one hex digit:

1001 1100
   9    C   -> 0x9C

To go from hex to decimal, multiply each digit by its power of sixteen and add. To go from decimal to hex, divide repeatedly by sixteen and read the remainders from last to first.

Common pitfalls

  • Forgetting the prefix. 10 means ten in decimal, two in binary, and sixteen in hex. Context or a prefix (0x, 0b) tells you which. Without one, a string of digits is ambiguous.
  • Leading zeros matter for width, not value. 0x0F and 0xF are both fifteen, but byte oriented formats pad to two digits so each byte stays two characters wide.
  • Grouping bits from the wrong end. Always group binary into nibbles from the right. Grouping from the left misaligns the final partial group and gives the wrong hex.
  • Mixing the letters. Hex letters stop at F. Seeing a G or higher means the value is not hexadecimal.

When you want to check your work or convert larger numbers quickly, the Number Base Converter shows binary, octal, decimal, and hex side by side. If you are decoding bytes that arrived as text, the Base64 Encode / Decode tool pairs well, and for hashing those bytes the Hash Generator reports its output in hex too.