2 / 6 · Intro
Bytes, hexadecimal and text
The letter A is stored as 65, or 01000001. In UTF-8 a Korean syllable such as U+D55C takes three bytes.
Jump to the 10 practice questionsYou will learn
- Find the code of a letter in ASCII and its bytes in UTF-8
Before this: Bits and binary numbers
1Explain
Six letters or three syllables
An everyday puzzle first. (This is a made-up example.) One student types the word Korean in a chat, six letters. A friend types the same word in Korean script, which takes three syllables. Which message is bigger? Probably not the one you would guess. Saved as UTF-8, the six English letters are 6 bytes. The three Korean syllables are 9 bytes, three each. RFC 3629, the standard that defines UTF-8, uses exactly this word as one of its examples and spells out its bytes: ED 95 9C EA B5 AD EC 96 B4.
const enc = new TextEncoder();
console.log(enc.encode("Korean").length);
console.log(enc.encode("한국어").length);6
92Explain
Two hex digits for every byte
Those pairs of letters and digits are hexadecimal, or hex. Long rows of 0s and 1s are hard to read, so programmers use this shorthand. Hex has sixteen digits: 0 to 9, then A to F for ten to fifteen. One hex digit covers exactly four bits, so a byte is always two hex digits. Split 01000001 into 0100 and 0001. Those are 4 and 1, so the byte is 0x41, where 0x only says "this is hex". In decimal it is 4 × 16 + 1 = 65.
3Problem
0x41
What is 0x41 in decimal?
Hint
4 × 16 + 1.
Solution
Answer: 65
Hex place values use powers of 16, so the first digit contributes sixteen times its value: 4 × 16 + 1 = 65.
4Problem
One hex digit
How many bits does one hex digit stand for?
bits
Hint
Sixteen values: 2 to the power what?
Solution
Answer: 4 bits
16 = 2⁴, so one hex digit is exactly four bits.
5Explore
Hex in colour codes
You have probably met hex without noticing. Colours on web pages are often written as #RRGGBB, one byte each for red, green and blue light (MDN). #FF0000 is full red: 255 in the first byte, 0 in the other two. #FFFFFF is white. The largest byte, 11111111, is 0xFF, and 0xFF = 15 × 16 + 15 = 255.
Each pair of hex digits is one byte: red, green and blue, from 0 to 255.
const hex = "#FF0000";
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
console.log(r + " " + g + " " + b);255 0 06Problem
0xFF
What is 0xFF in decimal?
Hint
15 × 16 + 15.
Solution
Answer: 255
F represents 15, so use it in both the sixteens and units places: 15 × 16 + 15 = 255.
7Explore
A is 65 because everyone agreed it would be
A computer has no idea what a letter is. It stores a number, and everyone agrees in advance which number stands for which letter. The agreement used on early networks was ASCII, and RFC 20 set it out for the ARPANET in 1969. ASCII uses 7 bits, so it has 128 codes. Capital A is 65, B is 66, and so on up to Z at 90. Small a is 97. That is exactly 32 more than A, a difference of a single bit.
Drag to turn. Scroll or pinch to zoom.
8Explain
Codes of letters
for (const ch of ["A", "B", "Z", "a", "0"]) {
console.log(ch + " " + ch.charCodeAt(0));
}A 65
B 66
Z 90
a 97
0 48charCodeAt gives the agreed number for each character. The digit 0 is the code 48, not the number 0.
9Problem
The code for C
A is 65 in ASCII. What number is C?
Hint
B is 66.
Solution
Answer: 67
Capital letters run in order: A 65, B 66, C 67.
10Problem
Capital and small
How much bigger is the code for a than the code for A?
Hint
97 − 65.
Solution
Answer: 32
97 − 65 = 32: one bit, the 32s place, tells capital and small letters apart.
11Problem
Read 01000010
Which character is stored as 01000010?
Hint
01000010 is 66.
Solution
Answer: B
01000010 = 64 + 2 = 66, which is B.
12Problem
Codes in 7-bit ASCII
How many codes does 7-bit ASCII have?
codes
Hint
Seven bits: 2 to the power 7.
Solution
Answer: 128 codes
Each bit has two possible values and the seven positions vary independently, giving 2⁷ = 128 different codes.
13Explain
Why a Korean syllable needs three bytes
128 codes are plenty for English and nowhere near enough for Korean, Arabic, Chinese or emoji. Unicode gives every character in the world's writing systems its own number, called a code point, up to U+10FFFF. UTF-8 (RFC 3629) is the most common way to turn those numbers into bytes, using one to four bytes per character. The 128 ASCII characters keep their old one-byte codes, so plain English text is identical in ASCII and UTF-8. The RFC's table puts every code point from U+0800 to U+FFFF in the three-byte row, and the Korean syllables sit in that range.
| Character | Code point | UTF-8 bytes (hex) | Bytes |
|---|---|---|---|
| A | U+0041 | 41 | 1 |
| a | U+0061 | 61 | 1 |
| é | U+00E9 | C3 A9 | 2 |
| Korean syllable (U+D55C) | U+D55C | ED 95 9C | 3 |
| 😀 | U+1F600 | F0 9F 98 80 | 4 |
14Explore
Bytes of any character
TextEncoder turns text into UTF-8 bytes. The first line is the number of bytes, the second the bytes in hex.
const text = "한";
const bytes = new TextEncoder().encode(text);
console.log(bytes.length);
console.log([...bytes].map((b) => b.toString(16).toUpperCase().padStart(2, "0")).join(" "));3
ED 95 9C15Problem
A Korean syllable
How many bytes does the Korean syllable U+D55C take in UTF-8?
bytes
Hint
U+0800 to U+FFFF take three bytes.
Solution
Answer: 3 bytes
U+D55C falls in UTF-8’s three-byte code-point range, so one written syllable uses the three bytes ED 95 9C.
16Explain
Garbled text is the right bytes read with the wrong agreement
A plain text file is these bytes in a row and nothing else. The word Hello saved as UTF-8 is five bytes: 48 65 6C 6C 6F in hex, or 72, 101, 108, 108, 111 in decimal. The two l's are the same byte twice. Now open that file, or one in Korean, with the wrong agreement, say an old Korean or Japanese encoding instead of UTF-8. The program reads the very same bytes as different characters. Nothing in the file is broken, and opening it again as UTF-8 brings the text back.
17Explain
Where the missing 34 GB went
Because 2 to the power 10 is 1024, close to 1000, people long used "kilobyte" for both. In December 1998 the IEC approved separate names, and NIST lists them: 1 kB is 1000 bytes but 1 KiB, a kibibyte, is 1024 bytes; 1 MB is 1,000,000 bytes but 1 MiB is 1,048,576 bytes. Drive makers count in powers of ten. A drive sold as 500 GB holds 500,000,000,000 bytes. An operating system that counts in powers of two divides by 1,073,741,824 and shows about 465.66 GiB.
18Problem
Bytes in 1 MiB
How many bytes are in 1 MiB?
bytes
Hint
1024 × 1024.
Solution
Answer: 1048576 bytes
MiB uses a binary power: 2²⁰ = 1,048,576 bytes. MB instead counts decimal millions, so 1 MB is 1,000,000 bytes.
19Problem
500 GB in GiB
A drive holds 500,000,000,000 bytes. About how many GiB is that? (one decimal place)
GiB
Hint
Divide by 1,073,741,824.
Solution
Answer: 465.7 GiB
Divide the byte total by the bytes in one GiB: 500,000,000,000 ÷ 1,073,741,824 ≈ 465.66, rounded to 465.7.
20Practice
A byte as two hex digits
Finish hexByte(n) so that it returns a number from 0 to 255 as exactly two capital hex digits: 65 gives 41 and 10 gives 0A.
41
FF
0Afunction hexByte(n) {
return n;
}
console.log(hexByte(65));
console.log(hexByte(255));
console.log(hexByte(10));
Hint
toString(16) gives hex; padStart(2, "0") adds the missing zero.
A solution
function hexByte(n) {
return n.toString(16).toUpperCase().padStart(2, "0");
}
console.log(hexByte(65));
console.log(hexByte(255));
console.log(hexByte(10));21Explain
Common mistakes
"1 KB is exactly 1000 bytes everywhere."
By the standard, kB means 1000 bytes and 1024 bytes is a KiB. In practice the old names are still used both ways. NIST notes that memory makers usually mean 2²⁰ bytes by a megabyte while storage makers mean 1,000,000, and that the old "1.44 MB" floppy disk used a third megabyte of 1,024,000 bytes. When the exact size matters, look for the i: KiB, MiB, GiB.
"My 500 GB drive arrived with 34 GB missing."
500,000,000,000 bytes ÷ 1,073,741,824 ≈ 465.66 GiB. Same bytes, different unit. 500 − 465.66 ≈ 34.34, and that gap is the "missing" 34.
"One character is always one byte."
In UTF-8 that holds only for the 128 ASCII characters. é takes 2 bytes, a Korean syllable 3 and 😀 4, so a limit counted in bytes fills up about three times faster with Korean text than with English.
"The character 7 is stored as the number 7."
The character "7" is code 55, or 00110111. A program that reads "7" from text has to turn it into the number 7 before it can do arithmetic with it.
22
Summary
- One hex digit = 4 bits; a byte = two hex digits (0x00 to 0xFF).
- ASCII: A = 65, a = 97, 0 = 48; 128 codes in 7 bits.
- UTF-8 uses 1 to 4 bytes per character; ASCII text is unchanged.
- 1 kB = 1000 B, 1 KiB = 1024 B; 1 MiB = 1,048,576 B.