Hexadecimal

Binary is how a computer really thinks, but long strings of 0s and 1s are horrible for people to read. Quick — is 11010110 the same as 11011010? Miss a single bit and you have the wrong number entirely. So programmers write those same values in a shorthand that is far kinder to human eyes: hexadecimal, or hex for short.

Hex is just counting in base-16. Where our everyday place value gives each column ten times the value of the one to its right (ones, tens, hundreds), and binary makes each column worth twice the one to its right, hex makes each column worth sixteen times the one to its right: ones, sixteens, two-hundred-and-fifty-sixes, and so on.

Sixteen digits — so we borrow some letters

Base-ten needs ten symbols (09). Base-16 needs sixteen symbols — but we only have ten number-symbols to hand. So hex keeps 09 and then borrows the first six letters to stand for the values ten to fifteen:

Read that table as "these letters are just digits": A means ten, B means eleven, all the way to F, which means fifteen. Then 10 in hex is the very next number after F — which is sixteen.

The superpower: one hex digit = four bits

Here is why hex and computers are best friends. Four bits — called a nibble — can make exactly 2^4 = 16 different patterns, from 0000 to 1111. And base-16 has exactly sixteen digits. So every nibble matches up with exactly one hex digit — no leftovers, no rounding.

That means a whole byte (eight bits) is just two hex digits: split the byte down the middle into two nibbles, and write the hex digit for each half.

To convert binary to hex, you never have to add up big place values — you just chop the bits into groups of four (starting from the right) and swap each nibble for its hex digit. To go back, replace each hex digit with its four bits. That is why hex is called a compact shorthand for binary: it says the same thing in a quarter of the characters.

Because 16 = 2^4 is a power of two. Whenever a base is a power of two, its digits line up perfectly with a fixed-size block of bits, with nothing left over. Grouping in three bits gives you the eight digits of octal (base-8, since 8 = 2^3), which programmers used a lot in the past. Hex won out because a byte is eight bits: eight splits cleanly into two nibbles of four, so a byte is a tidy pair of hex digits — whereas eight bits split awkwardly into octal.

Denary to hex, and back — Run it

In TypeScript you rarely convert hex by hand. A number carries a built-in .toString(16) that writes it in base-16, and parseInt(h, 16) reads a hex string back into an ordinary number. Change n and press Run:

// Change this ordinary (denary) number, then press Run! const n: number = 255; const hex: string = n.toString(16).toUpperCase(); console.log(n + " in denary = " + hex + " in hex"); // ...and straight back again: const back: number = parseInt(hex, 16); console.log(hex + " in hex = " + back + " in denary");

Try 255 (it gives FF, the biggest byte), 16 (which becomes 10), and 10 (which becomes A).

Doing it the long way, so you understand the machine

The exam wants you to convert by hand too, so here is the same job done with real arithmetic — no magic .toString. To turn a number into hex we repeatedly take the remainder when dividing by 16, mapping any remainder of ten or more to a letter:

let n: number = 2748; const digits: string = "0123456789ABCDEF"; let hex: string = ""; while (n > 0) { const remainder: number = n % 16; // a value from 0 to 15 hex = digits[remainder] + hex; // stick its symbol on the front n = Math.floor(n / 16); // divide, throwing away the remainder } console.log("2748 in hex is " + hex); // should be ABC

And to read hex back to denary, each digit is worth its value times a growing power of 16. For the two-digit byte FF: \text{F}\times 16 + \text{F}\times 1 = 15\times 16 + 15 = 255.

All over the place, once you look. Colours on web pages are written as three bytes of hex — #FF8800 means red = FF (full), green = 88 (about half) and blue = 00 (none), giving a warm orange. Memory addresses shown by a debugger look like 0x7FFE0300 (the 0x is just a flag that says "hex follows"). MAC addresses, error codes and file "magic numbers" are all quoted in hex, precisely because it is a tidy, readable stand-in for the underlying bits.

In hex the letters are digits with values, not spelling. A is not "the letter A" — it is the number ten, C is twelve, and F is fifteen. And be careful with 10: hex 10 is sixteen, not ten, because that 1 sits in the "sixteens" column with a 0 in the "ones" column. When it is not obvious, people label the base: 10_{16} = 16_{10}.