# Hexadecimal

> The Hexadecimal is a base-16 number system that uses sixteen digits: 0-9 and A-F, where A-F represent the values 10 to 15.

## How does hexadecimal work and why is it used in computing?

The base-16 number system that includes digits 0-9 and letters A-F, where A-F represent the values 10 to 15. It's commonly used in computing as a shorthand for binary values, as every two hexadecimal digits represent one byte of data.

- Topic: Number Systems
- Type: concept, format

## Example

Wrong:

```js
const toHex = (r, g, b) =>
  '#' + [r, g, b].map((n) => n.toString(16)).join('');
toHex(255, 0, 8); // '#ff08'
```

Right:

```js
const toHex = (r, g, b) =>
  '#' + [r, g, b].map((n) => n.toString(16).padStart(2, '0')).join('');
toHex(255, 0, 8); // '#ff0008'
```

Padding each channel to two hex digits keeps the color six characters long.

## Common mistakes

- Converting RGB to hex without padding, so 8 becomes '8' instead of '08' and the color is broken.

## Key points

- Each byte converted to hex is padded to two digits with `padStart(2, '0')`.
- Hex strings are parsed with `parseInt(str, 16)`; `Number(str)` needs a `0x` prefix.

## Interview questions

### How many bits is one hex digit?

Four. One hex digit covers values 0 to 15, so two digits describe one byte (0 to 255).

### What does an 8-digit hex color mean in CSS?

The last two digits are alpha: `#00000080` is black at about 50% opacity.

## Related cards

- [Binary](https://wonderium.dev/engineering/binary.md)
- [Byte](https://wonderium.dev/engineering/byte.md)
- [Octal](https://wonderium.dev/engineering/octal.md)
- [HEX Color](https://wonderium.dev/css/hex-color.md)
- [Number.parseInt()](https://wonderium.dev/js/number-parse-int.md)

---

- Level: [Engineering](https://wonderium.dev/engineering.md)
- HTML version: https://wonderium.dev/engineering/hexadecimal
- Last updated: 2026-11-25
