code q&a
Resources About
Question:

What is hexadecimal?

Hexadecimal is the number system behind strings like #ff0000 and 3f9a that developers run into all the time. It writes numbers with 16 digits instead of 10, so you get 0 through 9 and then a through f, where a means 10 and f means 15. People also call it hex or base 16.

Counting in hex starts out the same as normal counting. You go from 0 to 9 and then keep going with a, b, c, d, e and f, which gets you to fifteen. Hex runs out of digits at sixteen, so sixteen is written as 10 the same way decimal writes ten as 10.

The left digit of a two-digit number counts tens in decimal and sixteens in hex. That makes 1a one sixteen plus ten, which is 26. The biggest two-digit hex number is ff, which is fifteen sixteens plus fifteen, or 255.

Decimal Binary Hex
9 1001 9
10 1010 a
15 1111 f
16 10000 10
26 11010 1a
255 11111111 ff

Developers like hex because each hex digit stands for exactly 4 bits and a byte is 8 bits, so one byte always fits in two hex digits. A byte that takes eight characters in binary, like 10110100, shrinks to b4 in hex. Decimal doesn’t line up with bytes that way, so you can’t look at a long decimal number and tell where one byte ends and the next begins. Base64 is another way to write bytes as text. It uses 64 characters, so its output comes out even shorter than hex.

You’ll see hex all over the place once you start looking. CSS colors like #ff0000 are three bytes in a row, one each for red, green and blue. The ff means full and the 00 means none, so that color is pure red. Hashes, Git commit IDs and the memory addresses that pop up in error messages while debugging are usually written in hex.

Programming languages use a 0x prefix to mark a number as hex, so 0xff in JavaScript or Python is just 255. Hex letters work in uppercase or lowercase, so FF and ff mean the same thing.

You might also like