Data Representation
How text (ASCII, Unicode, UTF-8), real numbers (IEEE 754 floats), images, sound and structured data become bytes.
Memory only holds bytes. Whether a particular byte means a letter, part of a number or the red channel of a pixel depends entirely on how the program interprets it: the encoding. Getting the interpretation wrong causes garbled text, rounding surprises and security bugs.
Text: from ASCII to Unicode
ASCII (1963) maps 128 characters to the numbers 0–127: English letters, digits, punctuation and control codes. A = 65, a = 97, 0 = 48, newline = 10.
That isn't enough for the world's languages, so Unicode assigns a number, called a code point, to every character in every script, plus emoji: over 150,000 of them. Code points are written U+0041 (A), U+20AC (€), U+1F600 (😀).
Unicode says which number; an encoding says how to store that number in bytes.
| Encoding | Bytes per character | Notes |
|---|---|---|
| UTF-8 | 1–4 | ASCII-compatible; the web's default (98%+ of pages) |
| UTF-16 | 2 or 4 | Used inside Java, JavaScript and Windows strings |
| UTF-32 | 4 | Fixed width; simple but wasteful |
How UTF-8 works
- Code points 0–127 use one byte, identical to ASCII, so old ASCII text is valid UTF-8.
é(U+00E9) takes 2 bytes,€3 bytes,😀4 bytes.- You can find character boundaries from any position (continuation bytes always begin
10).
String s = "héllo😀";
s.length(); // 7 (UTF-16 code units!)
s.codePointCount(0, s.length()); // 6 (actual characters)
s.getBytes(StandardCharsets.UTF_8).length; // 10 bytess = "héllo😀"
len(s) # 6 (code points)
len(s.encode("utf-8")) # 10 (bytes)
s.encode("utf-8") # b'h\xc3\xa9llo\xf0\x9f\x98\x80'
b"\xc3\xa9".decode("utf-8") # 'é'const s = "héllo😀";
s.length; // 7 (UTF-16 code units)
[...s].length; // 6 (code points)
new TextEncoder().encode(s).length; // 10 bytesMojibake (garbled text like é instead of é) happens when bytes written in one encoding are read as another. Always state the encoding explicitly when reading files or sending HTTP (Content-Type: text/html; charset=utf-8).
Real numbers: floating point
Most languages store real numbers in IEEE 754 floating point: scientific notation in binary.
| Type | Bits | Significant decimal digits | Range |
|---|---|---|---|
float (single) | 32 | ~7 | ±3.4 × 10³⁸ |
double | 64 | ~15–16 | ±1.8 × 10³⁰⁸ |
Why 0.1 + 0.2 ≠ 0.3
0.1 has no finite binary representation (just as 1/3 has no finite decimal one), so it's stored rounded. The tiny errors add up:
0.1 + 0.2; // 0.30000000000000004
0.1 + 0.2 == 0.3; // false
Math.abs((0.1 + 0.2) - 0.3) < 1e-9; // true: compare with a tolerance
new BigDecimal("0.1").add(new BigDecimal("0.2")); // 0.3 exactly0.1 + 0.2 # 0.30000000000000004
import math
math.isclose(0.1 + 0.2, 0.3) # True
from decimal import Decimal
Decimal("0.1") + Decimal("0.2") # Decimal('0.3')0.1 + 0.2; // 0.30000000000000004
Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON; // trueSpecial values: +Infinity, -Infinity, NaN ("not a number", from 0/0; NaN != NaN), and signed zero (-0.0). Full details: Floating point.
Endianness: byte order
A 32-bit integer occupies 4 bytes. In which order are they stored?
Most significant byte first: 0x12345678 is stored as 12 34 56 78. Used by network protocols ("network byte order") and many file formats.
Least significant byte first: stored as 78 56 34 12. Used by x86 and (by default) ARM, so by almost every CPU you'll use.
Endianness matters when you read binary files or network packets byte by byte. Use your language's helpers (ByteBuffer.order, Python struct/int.from_bytes, JS DataView).
Images, sound and everything else
- Images: a grid of pixels, each usually 3–4 bytes (red, green, blue, alpha, 0–255 each). A 1920 × 1080 RGBA frame is about 8 MB uncompressed, which is why formats like PNG (lossless) and JPEG/WebP/AVIF (lossy) exist.
- Sound: the waveform's amplitude sampled at regular intervals, for example 44,100 samples/second × 16 bits × 2 channels for CD audio (about 10 MB per minute before MP3/AAC/Opus compression).
- Structured data: serialisation formats turn objects into bytes: text formats (JSON, XML, CSV) are readable, binary formats (Protocol Buffers, MessagePack, Avro) are smaller and faster.
- Bytes have no meaning without an encoding.
- Unicode assigns code points; UTF-8 stores them in 1–4 bytes and is ASCII-compatible.
- String "length" can mean bytes, code units, code points or graphemes.
- IEEE 754 floats are approximate: compare with tolerances, use decimals or integers for money.
- Endianness is byte order; x86/ARM are little-endian, networks are big-endian.
QWhy does Java's String.length() return 2 for a single emoji?medium
Java strings are UTF-16. Code points above U+FFFF (most emoji) are stored as a surrogate pair of two 16-bit code units, and length() counts code units. Use codePointCount for code points.
QWhat's the difference between UTF-8 and Unicode?easy
Unicode is the character set: it maps characters to numbers (code points). UTF-8 is one encoding of those numbers into bytes; UTF-16 and UTF-32 are others.