DevToys Pro

free web developer tools

Blog
Rate us:
Try browser extension:
← Back to Blog

Number Base Conversion: Hex, Binary, Octal, and Decimal for Developers

12 min read

You're debugging a memory dump and see 0x7FFFFFFF. A CSS color is #FF5733. A file permission is 0755. A network mask is 11111111.11111111.11111111.00000000. All of these are numbers in different bases—and understanding how to convert between them is essential for developers. This guide covers practical number base conversion with real-world examples.

The Four Number Bases Developers Use

BaseNameDigitsPrefixCommon Use
2Binary0, 10bBitwise operations, flags, masks
8Octal0-70o or 0Unix file permissions
10Decimal0-9(none)Human-readable numbers
16Hexadecimal0-9, A-F0x or #Colors, memory addresses, bytes

Quick Reference: Common Values

Decimal   Binary      Octal   Hex
-------   --------    -----   ---
0         0b0         0o0     0x0
1         0b1         0o1     0x1
7         0b111       0o7     0x7
8         0b1000      0o10    0x8
10        0b1010      0o12    0xA
15        0b1111      0o17    0xF
16        0b10000     0o20    0x10
255       0b11111111  0o377   0xFF
256       0b100000000 0o400   0x100
65535     (16 bits)   —       0xFFFF

Use the Number Base Converter to quickly convert between any bases without manual calculation.

Hexadecimal: Colors, Bytes, and Memory

CSS Color Codes

Hex color codes represent RGB values where each pair of hex digits is one color channel (0-255):

#FF5733
 ││││││
 ││││└┴─ Blue:  0x33 = 51
 ││└┴─── Green: 0x57 = 87
 └┴───── Red:   0xFF = 255

# Breaking down common colors:
#FFFFFF = rgb(255, 255, 255) = White
#000000 = rgb(0, 0, 0)       = Black
#FF0000 = rgb(255, 0, 0)     = Red
#00FF00 = rgb(0, 255, 0)     = Green
#0000FF = rgb(0, 0, 255)     = Blue
#808080 = rgb(128, 128, 128) = Gray

Convert hex to decimal for each channel:

# Hex digit values: 0-9 = 0-9, A-F = 10-15
# Two hex digits = first × 16 + second

FF = 15 × 16 + 15 = 240 + 15 = 255
57 = 5 × 16 + 7   = 80 + 7   = 87
33 = 3 × 16 + 3   = 48 + 3   = 51

# So #FF5733 = rgb(255, 87, 51)

Memory Addresses

Memory addresses are displayed in hexadecimal because they map cleanly to binary (each hex digit = 4 bits):

# 32-bit address
0x7FFFFFFF = 0111 1111 1111 1111 1111 1111 1111 1111
           = 2,147,483,647 (max signed 32-bit int)

# 64-bit address
0x00007FFFFFFFFFFF = typical user-space address limit

# Common memory patterns in debugging:
0xDEADBEEF  # Often used as magic number / uninitialized memory
0xCAFEBABE  # Java class file magic number
0xFEEDFACE  # Mach-O binary magic number (macOS)
0xBAADF00D  # Windows LocalAlloc uninitialized heap
0xCDCDCDCD  # MSVC debug heap uninitialized

Byte Values and Data

# One byte = 8 bits = 2 hex digits
# Range: 0x00 to 0xFF (0 to 255)

# ASCII in hex:
'A' = 0x41 = 65
'a' = 0x61 = 97
'0' = 0x30 = 48
' ' = 0x20 = 32

# Viewing file bytes (hexdump):
$ xxd file.txt | head -1
00000000: 4865 6c6c 6f20 576f 726c 640a            Hello World.
          ↑↑↑↑ ↑↑↑↑ ↑↑↑↑ ↑↑↑↑ ↑↑↑↑
          H e  l l  o     W o  r l  d \n

Binary: Bitwise Operations and Flags

Understanding Bit Positions

# Bit positions (right to left, starting at 0):
Position:  7   6   5   4   3   2   1   0
Value:    128  64  32  16   8   4   2   1

# Example: 0b11010110 = 214
128 + 64 + 0 + 16 + 0 + 4 + 2 + 0 = 214

# Quick mental math: powers of 2
2^0 = 1      2^4 = 16     2^8 = 256
2^1 = 2      2^5 = 32     2^9 = 512
2^2 = 4      2^6 = 64     2^10 = 1024 (1K)
2^3 = 8      2^7 = 128    2^20 = 1M, 2^30 = 1G

Bitwise Operations

# AND (&) - both bits must be 1
  0b1100
& 0b1010
--------
  0b1000

# OR (|) - either bit can be 1
  0b1100
| 0b1010
--------
  0b1110

# XOR (^) - bits must be different
  0b1100
^ 0b1010
--------
  0b0110

# NOT (~) - flip all bits
~ 0b1100 = 0b0011 (simplified)

# Left shift (<<) - multiply by 2^n
0b0001 << 3 = 0b1000  # 1 × 8 = 8

# Right shift (>>) - divide by 2^n
0b1000 >> 2 = 0b0010  # 8 ÷ 4 = 2

Bit Flags in Practice

# Define flags as powers of 2
FLAG_READ    = 0b0001  # 1
FLAG_WRITE   = 0b0010  # 2
FLAG_EXECUTE = 0b0100  # 4
FLAG_DELETE  = 0b1000  # 8

# Combine flags with OR
permissions = FLAG_READ | FLAG_WRITE  # 0b0011 = 3

# Check flag with AND
hasRead = (permissions & FLAG_READ) != 0   # True
hasExec = (permissions & FLAG_EXECUTE) != 0 # False

# Add flag with OR
permissions |= FLAG_EXECUTE  # 0b0111 = 7

# Remove flag with AND + NOT
permissions &= ~FLAG_WRITE   # 0b0101 = 5

# Toggle flag with XOR
permissions ^= FLAG_READ     # Flip read bit

Network Subnet Masks

# Subnet mask: consecutive 1s followed by 0s
/24 = 255.255.255.0
    = 11111111.11111111.11111111.00000000

/16 = 255.255.0.0
    = 11111111.11111111.00000000.00000000

/8  = 255.0.0.0
    = 11111111.00000000.00000000.00000000

# CIDR notation = count of leading 1 bits
# /24 means 24 bits for network, 8 bits for hosts
# 2^8 - 2 = 254 usable host addresses

Octal: Unix File Permissions

Unix file permissions use octal because each digit maps to exactly 3 bits (rwx):

# Each octal digit = 3 permission bits
# r=read(4), w=write(2), x=execute(1)

Octal  Binary  Meaning
-----  ------  -------
0      000     ---  (no permissions)
1      001     --x  (execute only)
2      010     -w-  (write only)
3      011     -wx  (write + execute)
4      100     r--  (read only)
5      101     r-x  (read + execute)
6      110     rw-  (read + write)
7      111     rwx  (all permissions)

Common Permission Patterns

# Three digits: owner / group / others
chmod 755 script.sh
       │││
       ││└─ others: r-x (5) = read + execute
       │└── group:  r-x (5) = read + execute
       └─── owner:  rwx (7) = all permissions

# Common permissions:
644 = rw-r--r--  # Standard file (owner rw, others read)
755 = rwxr-xr-x  # Executable/directory (owner all, others rx)
600 = rw-------  # Private file (owner only)
700 = rwx------  # Private directory/executable
777 = rwxrwxrwx  # Full access (usually bad practice!)
400 = r--------  # Read-only by owner (SSH keys)

Symbolic to Numeric Conversion

# Convert symbolic to octal
ls -l file.txt
-rw-r--r-- 1 user group 1234 Jan 1 12:00 file.txt
 │││││││││
 │││││││└┴── others: r-- = 4
 ││││││└──── group:  r-- = 4
 │││└┴┴───── owner:  rw- = 6
 └┴┴──────── type:   -   = regular file

Result: 644

# Common type indicators:
-  = regular file
d  = directory
l  = symbolic link
c  = character device
b  = block device

Conversion Techniques

Decimal to Binary (Division Method)

# Convert 42 to binary
42 ÷ 2 = 21 remainder 0
21 ÷ 2 = 10 remainder 1
10 ÷ 2 = 5  remainder 0 Read
5  ÷ 2 = 2  remainder 1 upward
2  ÷ 2 = 1  remainder 0
1  ÷ 2 = 0  remainder 1

42 = 0b101010

Binary to Hex (Grouping Method)

# Group binary digits in sets of 4 (from right)
0b 1010 1100 1111 0001

   A    C    F    1

Result: 0xACF1

# Reverse: expand each hex digit to 4 bits
0xDEAD = 0b 1101 1110 1010 1101
            D    E    A    D

Octal to Binary (Grouping Method)

# Group binary digits in sets of 3 (from right)
0b 111 101 101

   7   5   5

Result: 0o755

# Reverse: expand each octal digit to 3 bits
0o644 = 0b 110 100 100
           6   4   4

Programming Language Conversions

# JavaScript
parseInt('FF', 16)    // 255 (hex to decimal)
parseInt('11111111', 2)  // 255 (binary to decimal)
(255).toString(16)    // 'ff' (decimal to hex)
(255).toString(2)     // '11111111' (decimal to binary)

# Python
int('FF', 16)         # 255
int('11111111', 2)    # 255
hex(255)              # '0xff'
bin(255)              # '0b11111111'
oct(255)              # '0o377'

# Literals in code
const dec = 255;
const hex = 0xFF;     // Same as 255
const bin = 0b11111111; // Same as 255
const oct = 0o377;    // Same as 255

Real-World Debugging Scenarios

Reading Error Codes

# Windows HRESULT: 0x80070005
# Break down:
# 0x8... = failure (high bit set)
# 0x.007... = facility code 7 (Win32)
# 0x....0005 = error code 5 (ACCESS_DENIED)

# HTTP status in hex logs:
0x191 = 401 (Unauthorized)
0x194 = 404 (Not Found)
0x1F4 = 500 (Internal Server Error)

UUID/GUID Analysis

# UUID: 550e8400-e29b-41d4-a716-446655440000
#       xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
#                     │    │
#                     │    └─ Variant (8-b = RFC 4122)
#                     └────── Version (4 = random)

# Version 4 UUID: M = 4
# Variant bits: N starts with 10 (binary)
# N = a = 1010 (starts with 10) ✓

IPv4 Address Manipulation

# IP address as 32-bit integer
192.168.1.100 =
  192 × 256³ + 168 × 256² + 1 × 256 + 100
= 3232235876
= 0xC0A80164

# Reverse: extract octets
0xC0A80164
C0 = 192 (first octet)
A8 = 168 (second octet)
01 = 1   (third octet)
64 = 100 (fourth octet)

Bitfield Extraction

# Extract bits from a packed value
# Example: RGB565 (16-bit color)
# RRRRRGGGGGGBBBBB

color = 0b1111100000011111  // Magenta-ish
//        RRRRRGGGGGGBBBBB

red   = (color >> 11) & 0b11111    // 5 bits
green = (color >> 5)  & 0b111111   // 6 bits
blue  = color & 0b11111            // 5 bits

// Scale to 8-bit (0-255)
red8   = (red * 255) / 31
green8 = (green * 255) / 63
blue8  = (blue * 255) / 31

Common Pitfalls

1. Octal Confusion in JavaScript

// Legacy octal (avoid!)
const bad = 010;   // 8, not 10! Leading zero = octal

// Modern octal (explicit)
const good = 0o10; // Clearly 8 in octal

// JSON trap: leading zeros are invalid
{ "value": 010 }   // Syntax error in JSON!

2. Signed vs Unsigned Interpretation

// 0xFF as signed 8-bit = -1
// 0xFF as unsigned 8-bit = 255

// JavaScript bitwise ops return signed 32-bit
0xFFFFFFFF | 0     // -1 (not 4294967295!)

// Use >>> 0 to get unsigned
(0xFFFFFFFF >>> 0) // 4294967295

3. Hex Case Sensitivity

// Hex digits A-F are case-insensitive for value
0xFF === 0xff  // true

// But strings might differ
'FF' !== 'ff'  // Different strings!

// Normalize when comparing
str.toUpperCase() === 'FF'

Practical Tools

For quick conversions without mental math:

  • Number Base Converter - Convert between binary, octal, decimal, and hexadecimal instantly in your browser

Command-line alternatives:

# Bash/zsh built-in
echo $((16#FF))      # Hex to decimal: 255
echo $((2#11111111)) # Binary to decimal: 255
printf '%x\n' 255    # Decimal to hex: ff
printf '%o\n' 255    # Decimal to octal: 377

# Python one-liner
python3 -c "print(hex(255))"   # 0xff
python3 -c "print(int('FF',16))" # 255

# bc calculator
echo "obase=16; 255" | bc      # FF
echo "ibase=16; FF" | bc       # 255

Summary

  • Hexadecimal (base 16): Used for colors (#FF5733), memory addresses (0x7FFFFFFF), and byte values. Each digit = 4 bits.
  • Binary (base 2): Used for bitwise operations, flags, and subnet masks. Essential for understanding low-level data manipulation.
  • Octal (base 8): Primarily used for Unix file permissions (chmod 755). Each digit = 3 bits.
  • Decimal (base 10): Human-readable, used for general computation and display.

The key insight: hex and octal exist because they map cleanly to binary—hex groups 4 bits, octal groups 3 bits. Use the Number Base Converter for quick conversions when debugging or working with different number representations.