Skip to content
systemcoding.dev
Fundamentals

Convert Between Binary, Decimal and Hex by Hand

Convert numbers between binary, decimal and hexadecimal on paper with place values, repeated division and nibble grouping, then check the result in C.

5 min read
Summarize in AI
Flat illustration of a hex dump with one byte highlighted, with the C, Rust and Zig logos.

A calculator converts number bases in a second, so why learn to do it by hand? Because you will read hex dumps, register values and bit masks all day, and the fastest way to understand them is to see the structure in the digits instead of looking each one up. Six small procedures cover every conversion between binary, decimal and hexadecimal, and the two that matter most are the shortcuts between binary and hex. This post walks through each with a worked example, then checks the answers with a short C program built with GCC 14. It assumes you know what a bit is and that hex digits run from 0 to F, as covered in the posts on binary numbers and hexadecimal.

The three tools you need

Everything below uses only three ideas.

  1. Place values. In binary the places are powers of two: 1, 2, 4, 8, 16, 32, 64, 128. In hex they are powers of sixteen: 1, 16, 256, 4096. In decimal they are powers of ten.

  2. Repeated division. To write a number in base b, divide by b, keep the remainder, and repeat on the quotient until it is zero. The remainders read from last to first are the digits.

  3. Nibble grouping. One hex digit is exactly four bits, so binary and hex convert by grouping, with no arithmetic at all.

Memorise the eight place values of a byte, 128 64 32 16 8 4 2 1, and the sixteen four-bit patterns, 0000 for 0 up to 1111 for F. With those in your head, most conversions take seconds.

Binary to decimal

Write the place values over the bits, and add the weights where the bit is 1. Convert 11001000:

Place1286432168421
Bit11001000

The set bits are at 128, 64 and 8, and 128 plus 64 plus 8 is 200. So 11001000 is 200 in decimal. If a long binary number is unwieldy, convert to hex first with the grouping method below and then convert the hex, since fewer digits means fewer slips.

Decimal to binary

There are two ways to do this, and either works. The first is repeated division by two. Divide 200 by 2 and keep the remainders:

NumberDivided by 2Remainder
2001000
100500
50250
25121
1260
630
311
101

Read the remainders from the bottom up: 1, 1, 0, 0, 1, 0, 0, 0, which is 11001000, as before.

The second way is subtraction against the place values, which is quicker for a byte you can see at a glance. Start at the largest weight that fits. For 200, 128 fits, leaving 72. The next weight 64 fits, leaving 8. The weights 32 and 16 do not fit, 8 fits and leaves 0, and the rest do not fit. Putting a 1 where a weight fitted and a 0 where it did not gives 11001000 again. The subtraction method needs the powers of two in your head, so it is worth drilling them.

Binary to hex

Group the bits in fours starting from the right, padding the leftmost group with zeros if it is short, then replace each group with its hex digit. For 11001000 the groups are 1100 and 1000. The first is 8 plus 4, which is 12, or C. The second is 8, so 8. The result is 0xC8.

For a longer number such as 1011011101 there are ten bits, so pad on the left to twelve: 0010 1101 1101. That reads 2, D, D, so the value is 0x2DD. The padding zeros do not change the value.

Hex to binary

Replace each hex digit with its four bits, and keep all four, including leading zeros inside the number. For 0x7F, the digit 7 is 0111 and F is 1111, so the byte is 01111111. Drop leading zeros of the whole number only if you do not care about the width: 0x07 is 0000 0111, a full byte, and calling it 111 loses the information that it is one byte wide.

Hex to decimal

Multiply each digit by its place value and add. Convert 0xC8: the C is 12 and sits in the sixteens place, and the 8 sits in the units place, so 12 times 16 plus 8 is 192 plus 8, which is 200. Convert 0x1F4: 1 times 256, plus 15 times 16, plus 4, which is 256 plus 240 plus 4, giving 500.

For a quick sanity check, remember that the top digit of a two-digit hex number is worth sixteen, so 0x40 is 64, 0x80 is 128 and 0xFF is 255, one less than 256.

Decimal to hex

Use repeated division by 16, and write remainders of 10 to 15 as A to F. Convert 500:

NumberDivided by 16RemainderDigit
5003144
31115F
1011

Reading the digits from the bottom up gives 1F4, which matches the previous example. The alternative is to convert the number to binary first and group, which is often faster when the number is under 256.

Checking your work in C

Do the conversions on paper, then confirm with a program. The one below prints each value in decimal, hex and as eight bits. It was written for GCC 14 and works as C11 or later; it uses only the standard library.

convert_check.cc
#include <stdio.h>

static void print_byte_bits(unsigned value)
{
    for (int i = 7; i >= 0; i--) {
        putchar(((value >> i) & 1u) ? '1' : '0');
    }
}

int main(void)
{
    unsigned values[] = { 200u, 0x7Fu, 0xC8u, 45u };

    for (size_t i = 0; i < sizeof values / sizeof values[0]; i++) {
        printf("dec %3u  hex 0x%02X  bin ", values[i], values[i]);
        print_byte_bits(values[i]);
        putchar('\n');
    }
    return 0;
}

The function prints from bit 7 down to bit 0, so the text reads in the same order as a hand-written binary number. The %3u pads the decimal to three characters so the columns line up, and %02X prints at least two upper-case hex digits.

Text
dec 200  hex 0xC8  bin 11001000
dec 127  hex 0x7F  bin 01111111
dec 200  hex 0xC8  bin 11001000
dec  45  hex 0x2D  bin 00101101

The second row confirms that 0x7F is 127, one less than 128, with every bit set except the top. The third row reuses 0xC8 and shows that 200 and 0xC8 are the same value written two ways, which the compiler stores identically. The fourth row is the number 45 from the binary numbers post: 00101101 is 32 plus 8 plus 4 plus 1, and 0x2D is 2 times 16 plus 13.

A quick reference for a byte

Keep this small table in mind. It lets you spot values without calculating.

HexDecimalBinaryWhy it matters
0x0F1500001111low nibble mask
0xF024011110000high nibble mask
0x7F12701111111largest signed byte
0x8012810000000top bit alone
0xFF25511111111largest unsigned byte

Common mistakes

  • Reading the remainders from first to last instead of last to first. The first remainder is the least significant digit.

  • Forgetting that hex digits A to F stand for 10 to 15 when multiplying, and using 1 and the next digit.

  • Grouping bits from the left instead of the right. Always align at the least significant bit, and pad on the left.

  • Dropping the leading zeros of a nibble. 0x0A is 0000 1010, not 1010 followed by a lost nibble.

Taggedcbeginner
Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Linux logos.
Systems Programming

How argc and argv Reach main in a C Program

The kernel copies argc and argv onto the new process's stack on execve, then startup code hands them to main. See the layout and measure it with a program.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.