Find a course

Module 3: Hexadecimal and OctalLesson 5 of 1112 min

Hexadecimal Basics

Learn the sixteen hex digits 0 to F, convert hexadecimal numbers to decimal and back, and see why programmers prefer hex to long binary strings.

Step 1 of 12Learn

Sixteen digits

Binary is perfect for machines and painful for people. Quick: is 1101011010110101 the same as 1101011011110101? Long strings of 0s and 1s are hard to read, copy and compare. Programmers needed a shorter way to write binary, and the answer is hexadecimal, or hex: base 16.

Base 16 needs sixteen digits. The usual 0 through 9 cover the first ten, and then letters take over:

HexABCDEF
Decimal101112131415

Upper or lowercase both work: ff and FF are the same. Counting in hex goes 0, 1, ..., 9, A, B, C, D, E, F, and then the ones place is full, so you carry: 10, which in hex means sixteen. After 1F comes 20, which means 32.

Place values in hex are powers of 16: 1, 16, 256, 4,096 and so on. To make the base clear, hex numbers are often written with a 0x prefix (0x2F), a # in web colors (#2F8AC4) or a small 16 after them (2F_16). Without one of these clues, "10" is ambiguous: it could be ten, two or sixteen depending on the base.

The big advantage of hex, which you'll explore in the next lesson, is that one hex digit stands for exactly four bits. A byte, eight bits, is always exactly two hex digits.

Step 2 of 12Diagram

Taming a long binary string

Taming a long binary stringProcess diagram: 1101011010110101 → 1101 0110 1011 0101 → D 6 B 5 → 0xD6B5.110101101-01101011101 01101011 0101D 6 B 50xD6B5

Tap a shape with to see more.

1101011010110101

16 bits: hard to read or compare.

1101 0110 1011 0101

Group into fours from the right.

D 6 B 5

Each group of four bits is one hex digit.

0xD6B5

Four characters instead of sixteen.

Step 3 of 12Practice · Match the pairs

Match each hex digit to its decimal value.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. A
  2. C
  3. F
  4. 9
  5. E

Step 4 of 12Learn

Hex to decimal

Converting hex to decimal uses the same place value method as every base: multiply each digit by its place value and add.

The places from right to left are 160 = 1, 161 = 16, 162 = 256, 163 = 4,096.

Example 1: 0x2F

2 × 16 + F × 1 = 2 × 16 + 15 × 1 = 32 + 15 = 47

Example 2: 0xFF

15 × 16 + 15 × 1 = 240 + 15 = 255. Two hex digits reach exactly the maximum of one byte, which isn't a coincidence.

Example 3: 0x1A3

1 × 256 + A × 16 + 3 × 1 = 256 + 160 + 3 = 419

The two errors to watch for are forgetting to turn letters into their values (treating the A in 1A3 as if it weren't a number) and using powers of 10 out of habit. The second place in hex is worth 16, not 10, so 0x20 is 32, not 20.

With practice you'll memorize a few landmarks: 0x10 = 16, 0x20 = 32, 0x40 = 64, 0x80 = 128, 0xFF = 255, 0x100 = 256.

Step 5 of 12Diagram

Decoding 0x2A

Decoding 0x2AParts diagram: 0x2A = 42, 2 in the 16s place = 32, A in the 1s place = 10.0x2A = 422 in the 16splace = 32A in the 1splace = 10

Tap a shape with to see more.

32 + 10 = 42.
0x2A = 42
2 in the 16s place = 32

2 × 16

A in the 1s place = 10

A is the hex digit for ten.

Step 6 of 12Practice · Work it out

Convert the hexadecimal number 0x3C to decimal.

Step 7 of 12Learn

Decimal to hex

To convert decimal to hex, use either of the two methods from binary, now with 16 instead of 2.

Divide by 16. Divide repeatedly by 16, write each remainder as a hex digit (10 to 15 become A to F), and read the remainders bottom to top.

Convert 200:

  • 200 ÷ 16 = 12 remainder 8
  • 12 ÷ 16 = 0 remainder 12, which is C

Bottom to top: C8. Check: 12 × 16 + 8 = 192 + 8 = 200.

Subtract multiples of place values. For 200: how many 16s fit? 16 × 12 = 192, which fits, while 16 × 13 = 208 is too big. So the 16s digit is 12 (C), and the leftover is 200 − 192 = 8. Result: C8.

For numbers above 255, the 256s place comes into play. Convert 700: 256 fits twice (512), leaving 188. Then 188 ÷ 16 = 11 (B) remainder 12 (C). So 700 = 0x2BC. Check: 2 × 256 + 11 × 16 + 12 = 512 + 176 + 12 = 700.

A common slip is writing a remainder of 10 to 15 as two decimal digits, like "128" for 200. Every place in hex holds exactly one character, so 12 must become C.

Step 8 of 12Practice · Chart

Hex place values grow 16 times each step

Tap the place worth 256.

Tap the bar that answers the question, then press Check.

Hex place values grow 16 times each stepBar chart with 4 categories: 16^0: 1; 16^1: 16; 16^2: 256; 16^3: 4,096.01,0002,0003,0004,0005,00016^016^116^216^3Place, from the right1162564,096
Hex place values grow 16 times each step
Place, from the rightPlace value
16^01
16^116
16^2256
16^34,096

Step 9 of 12Practice · Work it out

The decimal number 175 is written with two hex digits. What is the value, as a decimal number from 0 to 15, of its left digit (the 16s digit)?

Step 10 of 12Note

Step 11 of 12Practice · Code challenge

Write hexToDecimal(hex) that converts a string of hex digits (uppercase, such as "2F" or "1A3") to a decimal number. Use the string "0123456789ABCDEF" and its indexOf method to find each digit's value. Don't use parseInt.

Runs in a sandbox in your browser.
  • hexToDecimal("2F")
  • hexToDecimal("FF")
  • hexToDecimal("1A3")
  • hexToDecimal("10")
  • hexToDecimal("0")

Step 12 of 12Practice · Choose one

What is 0x40 in decimal?