Cybersecurity · Reference sheet

Number Systems: Binary, Decimal & Hex

How binary, decimal, and hexadecimal represent numbers, how to convert between them, and why bits, bytes, and hex underpin IP addresses, colors, and hex dumps in cybersecurity.

Download Word (.docx)

What number systems are and why they matter

A number system is just a set of rules for writing numbers using a fixed set of symbols. The one you grew up with is decimal (base 10): ten digits, 0 through 9. Computers, though, store everything as electrical states that are either on or off, so at the hardware level they count in binary (base 2): only 0 and 1. Because long strings of 1s and 0s are painful for humans to read, we compress them into hexadecimal (base 16), where a single symbol stands for four binary digits. The value never changes across bases, only the way it is written down. The number forty-two is 42 in decimal, 0b101010 in binary, and 0x2A in hex, all the same quantity.

This is foundational for cybersecurity because the raw material of the field is bytes. You read hex dumps when analyzing a file or malware sample, you reason in bits when subnetting a network or writing a firewall rule, you see hex in MAC addresses, memory addresses, and cryptographic keys, and you decode encodings (ASCII, Base64, URL-encoding) that all rest on these three bases. If you cannot fluently move between binary, decimal, and hex, tools like xxd, tcpdump, and a subnet calculator stay opaque. Learn the conversions once and every one of those tools starts speaking a language you understand.

The three bases at a glance

Each base has a radix (how many distinct digits it uses) and often a prefix that labels which base a written number is in. Decimal is the default and usually carries no prefix. Binary literals are commonly prefixed with 0b, and hexadecimal with 0x. The prefix matters: without it, 10 could mean ten, two, or sixteen depending on the base.

The same value, 42, written in all three bases:

BaseDigits it usesWritten as
Binary (base 2)0 and 1 (2 digits)0b101010
Decimal (base 10)0 through 9 (10 digits)42
Hexadecimal (base 16)0-9, then A-F for 10-15 (16 digits)0x2A

Always know your base

The symbols 10 mean ten in decimal, two in binary, and sixteen in hex. Identical digits, different values. When there is any chance of confusion, use the 0b and 0x prefixes so nobody guesses wrong. Leading zeros (writing 00001010 instead of 1010) do not change the value; they just show how many bits wide the number is, which matters when you are talking about a full byte.

Counting 0-15 in all three bases

Memorizing this table is the single highest-value thing you can do. Sixteen values is exactly what one hexadecimal digit and one 4-bit nibble (half a byte) can each hold, so this chart is the bridge between binary and hex.

One hex digit = one 4-bit nibble = the numbers 0 through 15:

DecimalHexBinary (4-bit)
000000
110001
220010
330011
440100
550101
660110
770111
881000
991001
10A1010
11B1011
12C1100
13D1101
14E1110
15F1111

The 4-bit trick

Because every hex digit maps to exactly four binary bits, you never convert a whole byte at once. Split the 8 bits into two nibbles and convert each with the chart above. 1100 1010 becomes C and A, so the byte is 0xCA. This is precisely why hex is the shorthand for binary in every tool you will use.

Bits, bytes, and place values

A bit is a single 0 or 1. A byte is 8 bits grouped together. In any base, each position is worth the radix raised to a power, and you find a number's value by multiplying each digit by its position value and adding them up. In binary the position values are the powers of two. Learn these eight and you can convert any byte in your head.

The place values inside one 8-bit byte (read a byte right-to-left, smallest first):

Power of 2Bit position in the byteValue (decimal)
2^7leftmost bit (bit 7, the MSB)128
2^6bit 664
2^5bit 532
2^4bit 416
2^3bit 38
2^2bit 24
2^1bit 12
2^0rightmost bit (bit 0, the LSB)1

Why a byte tops out at 255

Add every place value together: 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255. That is the largest value 8 bits can hold, so one byte represents 256 distinct values from 0 to 255 (that is 2 to the 8th power). This is exactly why each part of an IPv4 address, and each red/green/blue channel in a color, ranges from 0 to 255: each one is a single byte.

Converting between bases

Binary to decimal — line the place values up under the bits and add the ones sitting over a 1: 1 1 0 0 1 0 1 0 128 + 64 + 0 + 0 + 8 + 0 + 2 + 0 = 202 Decimal to binary — subtract the largest power of two that fits, then repeat with what is left. For 45: 32 fits (13 left), 8 fits (5 left), 4 fits (1 left), 1 fits (0 left). Put a 1 in those positions and 0 everywhere else, giving 00101101. Hex to decimal — multiply each hex digit by 16 raised to its position. 0xCA = (C x 16) + A = (12 x 16) + 10 = 192 + 10 = 202. Binary and hex — use the nibble trick from the chart. 1100 1010 splits into C and A, so 11001010 = 0xCA. All three of these examples describe the same byte: 11001010 = 0xCA = 202.

# echo prints the text and the | pipe feeds it into bc, a command-line calculator; bc reads and writes in whatever base you set: ibase = input base, obase = output base
$ echo "ibase=2; 11001010" | bc
202
$ echo "obase=2; 202" | bc
11001010
# hex input to bc must be UPPERCASE
$ echo "ibase=16; CA" | bc
202
# printf converts on the fly: %d = decimal, %x = hex, %o = octal
$ printf '%d\n' 0xCA
202
$ printf '%x\n' 202
ca
Let the shell do the arithmetic with bc and printf

Where hex shows up: bytes, colors, and MAC addresses

Hex is everywhere in security tooling because two hex digits describe exactly one byte, which keeps binary data compact and readable. A CSS/HTML color like #FF5733 is three bytes: red = 0xFF = 255, green = 0x57 = 87, blue = 0x33 = 51. A MAC address is 48 bits written as six hex octets, such as 00:1A:2B:3C:4D:5E, where the first three octets identify the hardware vendor (the OUI). And every byte in a captured packet or a file shows up as a pair of hex digits when you dump it.

Common places a security analyst meets hex:

Where you see itWhat it encodesExample
CSS / HTML color3 bytes: red, green, blue, each 0-255#FF5733
MAC address48 bits = 6 hex octets; first 3 = vendor OUI00:1A:2B:3C:4D:5E
Byte in a hex dumpone 8-bit byte shown as two hex digits0x48 = 'H'
Memory addressa location in RAM, seen in debuggers/crashes0x7ffee3b0
IPv6 group16 bits (2 bytes) per colon-separated group2001:0db8::1
# -n tells echo not to add a trailing newline, so xxd sees exactly the two bytes H and i
$ echo -n "Hi" | xxd
00000000: 4869 Hi
# 48 = 'H' (decimal 72), 69 = 'i' (decimal 105) -- each byte is one ASCII character
A hex dump shows the raw bytes of data next to their ASCII characters

Bits and bytes in IP addresses

An IPv4 address is a 32-bit number, split into four 8-bit octets (four bytes) written in decimal and separated by dots. Because each octet is one byte, each part legally ranges from 0 to 255, which is why 256.0.0.1 is invalid. Under the hood the dotted-decimal form is just binary in disguise. Take 192.168.1.1: 192 = 11000000 168 = 10101000 1 = 00000001 1 = 00000001 So the full address in binary is 11000000.10101000.00000001.00000001. Reading it as bits is what lets you see where the network part ends and the host part begins, which is the whole idea behind subnet masks.

IP addressing measured in bits and bytes:

ConceptBits / sizeExample
IPv4 address32 bits = 4 octets (4 bytes)192.168.1.1
One octet8 bits, range 0-255192 = 11000000
Subnet mask /24first 24 bits = network, last 8 = hosts255.255.255.0
IPv6 address128 bits = 8 groups of 16-bit hex2001:db8::1

Why analysts think in bits here

Subnetting, CIDR notation like /24, firewall allow/deny rules, and reading packet captures all depend on seeing an IP as its underlying bits. A /24 means the first 24 bits are fixed as the network and the remaining 8 bits vary. That gives 2 to the 8th = 256 addresses in the block, but two are reserved (the network address, ending in .0, and the broadcast address, ending in .255), leaving 254 usable for hosts. You cannot do that math in dotted-decimal alone; you have to picture the binary.

Try it yourself

A 10-row fictional network device inventory with a binary asset tag, an IP octet, and an RGB hex color per device, for practicing conversions between bases.

Get to the file first

Your browser saves the file to your Downloads folder. Move it into a labs folder and go there: mkdir -p ~/labs && mv ~/Downloads/device-inventory.csv ~/labs/ && cd ~/labs Then run cat device-inventory.csv and confirm you can see all 10 devices. On WSL, your Windows Downloads folder is /mnt/c/Users/<your-name>/Downloads instead of ~/Downloads.

Work each conversion by hand first (place values / nibble trick), then check yourself with printf or python: 1. Find the row for cam-07. Convert its binary_id to decimal, then to hex by splitting it into two nibbles. Check with python -c "print(int('<binary_id>', 2))" and printf '%x\n' <the decimal answer>. 2. Find the row for switch-01. Convert its ip_last_octet to binary and to hex. Check with python -c "print(bin(<n>))" and printf '%x\n' <n>. 3. Find the row for badge-reader-3. Its badge_color_hex is a 3-byte RGB color: two hex digits each for red, green, and blue. Convert just the red byte to decimal (check with printf '%d\n' 0x<red byte>), then state the smallest and largest decimal value any single byte in that column could ever hold, and explain why.