X Xerobit

Decimal to Binary — How to Convert Numbers Between Bases

Decimal to binary, binary to decimal, hex to binary — number base conversion explained step by step. Includes conversion tables, formulas, and code examples.

Mian Ali Khalid · · 7 min read
Use the tool
Number Base Converter
Convert between binary, octal, decimal, hexadecimal, and text (UTF-8). Handles arbitrary lengths. Per-byte and per-character views.
Open Number Base Converter →

Decimal is the number system humans use. Binary is the number system computers use. Converting between them is a core skill for understanding how computers represent data — whether you’re debugging a bitmask, reading memory dumps, or learning computer architecture.

Use the Number Base Converter on this site to convert between binary (base-2), decimal (base-10), hexadecimal (base-16), and octal (base-8) instantly.

Why computers use binary

Transistors, the fundamental building block of modern CPUs, are switching devices: they’re either on or off. That maps naturally to 1 and 0. Building circuits that represent 10 distinct values (for decimal) would require far more complex hardware than circuits that represent 2 values. Binary is the natural language of digital electronics.

From binary, everything else is derived:

  • Octal (base-8): 3 binary bits grouped together — used in Unix file permissions (chmod 755)
  • Hexadecimal (base-16): 4 binary bits grouped together — used for memory addresses, color codes, error codes
  • Decimal: the human-friendly representation we convert to for display

Decimal to binary conversion

Method 1: Repeated division by 2

Divide the decimal number by 2 repeatedly. Record each remainder (0 or 1). The binary number is the remainders read in reverse order.

Example: convert 43 to binary

DivisionQuotientRemainder
43 ÷ 2211
21 ÷ 2101
10 ÷ 250
5 ÷ 221
2 ÷ 210
1 ÷ 201

Read remainders bottom to top: 101011

Verify: 1×2⁵ + 0×2⁴ + 1×2³ + 0×2² + 1×2¹ + 1×2⁰ = 32 + 0 + 8 + 0 + 2 + 1 = 43

Method 2: Subtract largest power of 2

Find the largest power of 2 that fits in your number. Subtract it (write a 1). Repeat for the remainder. Write 0 for each power of 2 that doesn’t fit.

Example: convert 100 to binary

Powers of 2 up to 100: 64, 32, 16, 8, 4, 2, 1

  • 64 ≤ 100: write 1, remainder = 100 - 64 = 36
  • 32 ≤ 36: write 1, remainder = 36 - 32 = 4
  • 16 > 4: write 0
  • 8 > 4: write 0
  • 4 ≤ 4: write 1, remainder = 4 - 4 = 0
  • 2 > 0: write 0
  • 1 > 0: write 0

Binary: 1100100

Verify: 64 + 32 + 4 = 100

Binary to decimal conversion

Sum each bit multiplied by 2 raised to its position (starting at 0 from the right).

Example: convert 10110101 to decimal

Position76543210
Bit10110101
Value128032160401

128 + 32 + 16 + 4 + 1 = 181

Quick binary-to-decimal reference

BinaryDecimalHex
000000
000111
001022
001133
010044
010155
011066
011177
100088
100199
101010A
101111B
110012C
110113D
111014E
111115F

Memorize this table and you can convert any hex digit to 4 bits instantly.

Decimal to hexadecimal

Method: divide by 16 (like base-2 but divide by 16)

Example: convert 255 to hex

DivisionQuotientRemainder
255 ÷ 161515 → F
15 ÷ 16015 → F

Read bottom to top: FF

Verify: 15×16¹ + 15×16⁰ = 240 + 15 = 255

Common decimal to hex conversions

DecimalHexBinary
00x0000000000
1270x7F01111111
1280x8010000000
2550xFF11111111
2560x100100000000
655350xFFFF1111111111111111
655360x1000010000000000000000

These boundary values are significant in computing: 127 is the max signed 8-bit integer, 128 is where the sign bit flips, 255 is max unsigned 8-bit, 65535 is max unsigned 16-bit.

Hexadecimal to binary (and back)

This is the most useful conversion for programmers because it’s instantaneous once you know the 4-bit groups.

Hex to binary: replace each hex digit with its 4-bit binary equivalent.

0xFF = F  F
     = 1111 1111
     = 11111111

0xAD = A  D
     = 1010 1101
     = 10101101

0xDEADBEEF = D    E    A    D    B    E    E    F
           = 1101 1110 1010 1101 1011 1110 1110 1111

Binary to hex: group bits into groups of 4 from the right, convert each group.

1011011010 = 10 1101 1010
           = 2  D    A
           = 0x2DA

Pad the leftmost group with zeros if needed: 10 becomes 0010.

Octal conversion

Octal (base-8) groups binary into groups of 3 bits. Each octal digit represents 3 bits.

OctalBinary
0000
1001
2010
3011
4100
5101
6110
7111

Octal relevance today: Unix file permissions

chmod 755 means:

  • 7 = 111 binary = read(4) + write(2) + execute(1) for owner
  • 5 = 101 binary = read(4) + execute(1) for group
  • 5 = 101 binary = read(4) + execute(1) for others

chmod 644:

  • 6 = 110 = read + write for owner
  • 4 = 100 = read only for group
  • 4 = 100 = read only for others

Understanding octal-binary mapping makes Unix permissions immediately readable.

Two’s complement: negative binary numbers

Positive integers have a straightforward binary representation. Negative numbers in computer hardware use two’s complement notation.

To negate a binary number in two’s complement:

  1. Invert all bits (one’s complement)
  2. Add 1

Example: represent -5 in 8-bit two’s complement

  1. Start with 5: 00000101
  2. Invert all bits: 11111010
  3. Add 1: 11111011

-5 in 8-bit two’s complement = 11111011

Verify: 11111011 as unsigned = 128+64+32+16+8+0+2+1 = 251 = 256 - 5 ✓

Two’s complement is why 0xFF in a signed 8-bit integer is -1, not 255. This is a common source of bugs when mixing signed and unsigned integer types.

uint8_t a = 0xFF;   // 255 (unsigned)
int8_t  b = 0xFF;   // -1 (signed two's complement)

// In C, signed overflow is undefined behavior:
int8_t c = 127;
c++;  // Undefined behavior — don't do this

Number base conversion in programming

Python

# Decimal to other bases:
bin(255)   # '0b11111111'  — prefix 0b
oct(255)   # '0o377'       — prefix 0o
hex(255)   # '0xff'        — prefix 0x

# Strip prefix:
bin(255)[2:]  # '11111111'
format(255, 'b')   # '11111111'
format(255, '08b') # '11111111' (zero-padded to 8 bits)
format(255, 'x')   # 'ff'
format(255, 'X')   # 'FF' (uppercase)
format(255, 'o')   # '377'

# Convert from other bases to decimal:
int('11111111', 2)  # 255 (binary → decimal)
int('ff', 16)       # 255 (hex → decimal)
int('377', 8)       # 255 (octal → decimal)

JavaScript

// Decimal to other bases:
(255).toString(2)   // '11111111'
(255).toString(8)   // '377'
(255).toString(16)  // 'ff'

// Other bases to decimal:
parseInt('11111111', 2)  // 255
parseInt('ff', 16)       // 255
parseInt('377', 8)       // 255

// Zero-padded hex:
(255).toString(16).padStart(4, '0')  // '00ff'

C

#include <stdio.h>

int main() {
    int n = 255;
    printf("%d\n", n);   // Decimal: 255
    printf("%o\n", n);   // Octal:   377
    printf("%x\n", n);   // Hex:     ff
    printf("%X\n", n);   // Hex:     FF
    
    // Binary requires manual conversion:
    for (int i = 7; i >= 0; i--)
        printf("%d", (n >> i) & 1);
    printf("\n");  // Binary: 11111111
    
    return 0;
}

Bit manipulation fundamentals

Once you can convert between bases, bit manipulation becomes readable:

OperationSymbolExampleResult
AND&0b1010 & 0b11000b1000 (8)
OR|0b1010 | 0b11000b1110 (14)
XOR^0b1010 ^ 0b11000b0110 (6)
NOT~~0b1010 (8-bit)0b11110101 (245 unsigned, -11 signed)
Left shift<<1 << 38 (multiply by 2³)
Right shift>>16 >> 24 (divide by 2²)

Common patterns:

  • Check if bit n is set: (value >> n) & 1
  • Set bit n: value | (1 << n)
  • Clear bit n: value & ~(1 << n)
  • Toggle bit n: value ^ (1 << n)

Related posts

Related tool

Number Base Converter

Convert between binary, octal, decimal, hexadecimal, and text (UTF-8). Handles arbitrary lengths. Per-byte and per-character views.

Written by Mian Ali Khalid. Part of the Encoding & Crypto pillar.