Data Representation

How text (ASCII, Unicode, UTF-8), real numbers (IEEE 754 floats), images, sound and structured data become bytes.

Beginner 4 min readLesson 3 of 8 Interview Q&A

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.

EncodingBytes per characterNotes
UTF-81–4ASCII-compatible; the web's default (98%+ of pages)
UTF-162 or 4Used inside Java, JavaScript and Windows strings
UTF-324Fixed width; simple but wasteful

How UTF-8 works

byte 1byte 2byte 3byte 412340xxxxxxx110xxxxx10xxxxxx1110xxxx10xxxxxx10xxxxxx11110xxx10xxxxxx10xxxxxx10xxxxxx
The leading bits say how many bytes follow; continuation bytes start with 10.
  • 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 bytes

Mojibake (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.

0100000000001000signexponent (11 bits)fraction (52 bits)…
64-bit double: 3.0 = +1.5 × 2¹ (the fraction is truncated here; it continues with zeros)
value=(−1)sign×1.fraction×2exponent−1023\text{value} = (-1)^{\text{sign}} \times 1.\text{fraction} \times 2^{\text{exponent} - 1023}
TypeBitsSignificant decimal digitsRange
float (single)32~7±3.4 × 10³⁸
double64~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 exactly

Special 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?

Big-endian

Most significant byte first: 0x12345678 is stored as 12 34 56 78. Used by network protocols ("network byte order") and many file formats.

Little-endian

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.
Key takeaways
  • 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.
Interview questions
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.