About

3.1 Character Codes: BCD, EBCDIC, ASCII & Unicode

Theory: Character Codes: BCD, EBCDIC, ASCII & Unicode

A computer only stores bit patterns, but people type letters, digits and punctuation. A character code is simply an agreed table that maps each human-readable character to a fixed bit pattern, so that the keyboard, the memory, the printer and every other computer that uses the same table agree on what 01000001 means. This lesson follows the historical path of the main codes (Null & Lobur, Ch. 2): BCD → EBCDIC → ASCII → Unicode. Each step exists because the previous code ran out of room.

CodeBits per characterCharacters possibleWhere it is used
BCD (6-bit)626=642^6 = 64IBM mainframes, 1950s–60s
EBCDIC828=2562^8 = 256IBM mainframes (still today)
ASCII7 (+1 parity)27=1282^7 = 128Almost everything outside IBM mainframes
Unicode21-bit code space (stored as 8/16/32-bit units)1,114,1121{,}114{,}112 code pointsModern OSes, Java, the web

Binary-Coded Decimal (BCD)

Intuition: business computers mostly handled decimal amounts (money, quantities). Instead of converting a whole number to binary, BCD encodes each decimal digit separately in 4 bits. That makes conversion to and from printed decimal trivial and avoids binary-fraction rounding of amounts like 0.10.

The rule: each digit 0–9 becomes its 4-bit binary value. The six patterns 1010–1111 are never used for digits.

DigitBCDDigitBCD
0000050101
1000160110
2001070111
3001181000
4010091001

Mini-example

30510305_{10} in BCD is 0011 0000 0101 (3 → 0011, 0 → 0000, 5 → 0101). Compare with pure binary: 30510=1001100012305_{10} = 100110001_2. The BCD form is longer (12 bits vs 9) but every nibble can be read off as a decimal digit.

Zone and digit

When BCD is stored one digit per byte, the byte is split into two nibbles: the high nibble is the zone and the low nibble is the digit. The zone tells us how to interpret the digit:

Zone (high nibble)Meaning
1111unsigned digit
1100positive (+)
1101negative (−)

So +4+4 is stored as zone 1100, digit 0100: 1100 0100. In a multi-digit zoned decimal number, every digit gets zone 1111 except the last (rightmost) digit, whose zone carries the sign.

Packed BCD and the sign nibble

Storing a whole byte per digit wastes half the bits, so packed BCD puts two digits per byte and places the sign in a final sign nibble at the right-hand end: 1100 = +, 1101 = −, 1111 = unsigned. If the digits plus the sign nibble make an odd number of nibbles, a leading 0000 is added so the number fills whole bytes.

Mini-example

−26-26 in packed BCD: digits 0010 0110, sign 1101 → three nibbles, so pad on the left with 0000:

0000 0010 | 0110 1101   =  02 6D (hex)
 pad   2     6   sign(−)

EBCDIC

In 1964 IBM extended 6-bit BCD to the 8-bit Extended Binary-Coded Decimal Interchange Code (EBCDIC). It was one of the first widely used codes with both upper- and lowercase letters plus punctuation and control characters, and it is still used on IBM mainframes.

EBCDIC keeps the zone | digit idea: the high nibble (zone) picks a row of the code table and the low nibble (digit) picks a column. Some landmarks worth remembering:

Character(s)ZoneDigitsHex
space0100000040
a – i10000001–100181–89
j – r10010001–100191–99
s – z10100010–1001A2–A9
A – I11000001–1001C1–C9
J – R11010001–1001D1–D9
S – Z11100010–1001E2–E9
0 – 911110000–1001F0–F9

Notice the digits: zone 1111 + the BCD digit, exactly the “unsigned” zone from BCD. The letters are not contiguous (there are gaps after I and R), a leftover of punched-card codes.

Mini-example

“A” in EBCDIC: row 1100, column 0001 → 1100 0001 = C1. The digit “7”: 1111 0111 = F7.

ASCII

Other manufacturers adopted the 7-bit American Standard Code for Information Interchange (ASCII). BCD/EBCDIC came from punched-card codes; ASCII came from telecommunications (Telex) codes. It became the dominant code outside the IBM world.

With 7 bits there are 27=1282^7 = 128 codes, hex 00–7F:

Range (hex)Contents
00–1F, 7Fcontrol characters (NUL, LF = 0A, CR = 0D, ESC = 1B, DEL = 7F …)
20space
30–39digits “0”–“9”
41–5A“A”–“Z”
61–7A“a”–“z”

Three anchors let you derive almost any ASCII code: “0” = 30 hex (48), “A” = 41 hex (65), “a” = 61 hex (97). Count forward from the anchor. Upper and lower case differ by exactly 20 hex (32), i.e. a single bit (bit 5). In Excel, =CODE("A") gives 65 and =CHAR(65) gives “A”; =DEC2BIN(65) gives 1000001.

Mini-example

“K” is the 11th letter, so its code is 4116+10=4B16=751041_{16} + 10 = 4B_{16} = 75_{10}. In 7 bits: 4B16=100 101124B_{16} = 100\ 1011_2.

Parity bits (even and odd)

A 7-bit code fits in an 8-bit byte with one bit to spare. Early systems used that spare bit as a parity bit, the simplest error-detecting code. It is placed in the leftmost (8th) position.

Procedure:

  1. Write the 7-bit code and count its 1s.
  2. Even parity: parity bit = 1 if the count is odd, else 0. Odd parity: parity bit = 1 if the count is even, else 0.
  3. Prepend the parity bit.

To check a received byte, count all eight 1s: if the count does not have the agreed parity, an error has occurred.

Mini-example (as in the lecture)

A single parity bit detects any odd number of flipped bits but misses an even number: if two bits flip, the count of 1s keeps its parity and the error goes unnoticed. Parity can also only detect: it cannot tell you which bit is wrong. As hardware became more reliable, the parity bit was often dropped and the 8th bit used instead for an extended character set (128 more characters: accented letters, line-drawing and math symbols, Thai characters in TIS-620, etc.).

Unicode

Most of today’s systems use Unicode, which aims to give every character of every writing system a unique number. Java and many operating systems use it as their default character code. The original Unicode was a 16-bit code, and Null & Lobur describe its 16-bit code space in broad blocks:

Character typesLanguageNumber of charactersHex values
AlphabetsLatin, Greek, Cyrillic, etc.81920000–1FFF
SymbolsDingbats, mathematical, etc.40962000–2FFF
CJKChinese, Japanese, Korean phonetic symbols and punctuation40963000–3FFF
HanUnified Chinese, Japanese and Korean40,9604000–DFFF
Han expansion4096E000–EFFF
User defined4095F000–FFFE

Code points and planes. Each character is identified by a code point, written U+ followed by hex digits (e.g. U+0041). Modern Unicode (version 15) uses a 21-bit code space from U+0000 to U+10FFFF, divided into 17 planes of 216=65,5362^{16} = 65{,}536 code points each:

PlaneRangeContents
0 — Basic Multilingual Plane (BMP)U+0000 – U+FFFFalmost all commonly used characters
1 – 16 — supplementary planesU+10000 – U+10FFFFancient scripts, rare CJK, symbols, emoji, private use

Mini-example

“A” = U+0041 (plane 0, same value as ASCII 41). ”€” = U+20AC (plane 0, in the symbols block 2000–2FFF). ”😀” = U+1F600: the digits in front of the last four are “1”, so it lies in plane 1, outside the BMP.

A code point is a number; an encoding form says how that number is stored. UTF-16 stores every BMP character in one 16-bit unit and a supplementary character in two units (a surrogate pair); UTF-8 uses 1 to 4 bytes and stores ASCII characters in a single byte unchanged.

Converting text to codes: the general procedure

  1. Look up each character’s code (ASCII/EBCDIC table or Unicode code point), usually as hex.
  2. Convert each hex digit to 4 bits (ASCII: drop the leading 0 to get 7 bits).
  3. If a parity scheme is used, count the 1s and prepend the parity bit.
  4. Write the bytes in order, left to right as the text is read.

Decoding runs the same steps backwards: check parity, strip the parity bit, look up the 7-bit value.


Worked Examples

Exercise 1

Encode the word CAT in 7-bit ASCII. Give each character in decimal, hex and binary, then give the 8-bit bytes with (a) even parity and (b) odd parity.

Show solution ↓
Solution

Step 1: look up the codes. Using the anchor “A” = 411641_{16}: C is 2 letters after A, T is 19 letters after A.

CharDecimalHex7-bit binaryNumber of 1s
C6743100 00113
A6541100 00012
T8454101 01003

(Check T: 4116+19=4116+1316=5416=841041_{16} + 19 = 41_{16} + 13_{16} = 54_{16} = 84_{10} ✓.)

Step 2: (a) even parity. The parity bit makes the total number of 1s even, so it is 1 when the 7-bit count is odd.

CharCountParity bitByteHex
C3 (odd)11100 0011C3
A2 (even)00100 000141
T3 (odd)11101 0100D4

Step 3: (b) odd parity. The parity bit is the complement of the even-parity bit.

CharParity bitByteHex
C00100 001143
A11100 0001C1
T00101 010054

Check: even-parity C = 1100 0011 has four 1s (even) ✓; odd-parity A = 1100 0001 has three 1s (odd) ✓.

Exercise 2

Encode the message “OK” in ASCII with even parity. (This is the message used for all the recording codes in the next section.)

Show solution ↓
Solution

“O”: O is the 15th letter, so 4116+14=4116+E16=4F16=791041_{16} + 14 = 41_{16} + \text{E}_{16} = 4\text{F}_{16} = 79_{10}.

4F16=100 11112⇒five 1s (odd)⇒parity bit=14\text{F}_{16} = 100\ 1111_2 \quad\Rightarrow\quad \text{five 1s (odd)} \Rightarrow \text{parity bit} = 1

O = 1100 1111.

“K”: 4116+10=4B16=751041_{16} + 10 = 4\text{B}_{16} = 75_{10}.

4B16=100 10112⇒four 1s (even)⇒parity bit=04\text{B}_{16} = 100\ 1011_2 \quad\Rightarrow\quad \text{four 1s (even)} \Rightarrow \text{parity bit} = 0

K = 0100 1011.

Message: 11001111 01001011 (16 bits). Check: O has six 1s, K has four 1s, both even ✓.

In Excel: =CODE("O") → 79 and =DEC2BIN(79) → 1001111; then add the parity bit by hand.

Exercise 3

A receiver uses 7-bit ASCII with even parity. Which of these received bytes contain a detectable error? For the good ones, give the character.

(a) 1000 0011 (b) 0100 0001 (c) 1100 1001 (d) 0111 0001 (e) 1101 0111

Show solution ↓
Solution

Count all eight bits. Even parity requires an even count.

ByteNumber of 1sParity OK?7-bit codeCharacter
(a) 1000 00113✗ error——
(b) 0100 00012✓100 0001 = 41A
(c) 1100 10014✓100 1001 = 49I
(d) 0111 00014✓111 0001 = 71q
(e) 1101 01116✓101 0111 = 57W

Only (a) is detected as an error (it is the lecture’s example: “C” would be 1100 0011 under even parity, and the leading parity bit has been flipped).

“Parity OK” does not prove the byte is correct. If (e) had been sent as “U” = 0101 0101 (four 1s) and two bits flipped to give 1101 0111, the count would still be even and the error would pass unnoticed.

Exercise 4

Write each signed decimal number in (i) zoned decimal (one digit per byte) and (ii) packed BCD, giving binary and hex.

(a) +4+4 (b) +305+305 (c) −1265-1265

Show solution ↓
Solution

Rules: zoned decimal puts zone 1111 on every digit except the last, whose zone is the sign (1100 = +, 1101 = −). Packed BCD writes the digits two per byte and ends with a sign nibble, padding with a leading 0000 if needed.

(a) +4+4

  • Zoned: one digit, zone = + → 1100 0100 = C4.
  • Packed: digit 0100, sign 1100 → 0100 1100 = 4C.

(b) +305+305 (digits 0011, 0000, 0101)

  • Zoned: 1111 0011 | 1111 0000 | 1100 0101 = F3 F0 C5.
  • Packed: 3 digits + sign = 4 nibbles (even, no padding): 0011 0000 | 0101 1100 = 30 5C.

(c) −1265-1265 (digits 0001, 0010, 0110, 0101)

  • Zoned: 1111 0001 | 1111 0010 | 1111 0110 | 1101 0101 = F1 F2 F6 D5.
  • Packed: 4 digits + sign = 5 nibbles (odd) → pad with 0000:
0000 0001 | 0010 0110 | 0101 1101   =  01 26 5D
 pad   1      2    6      5   sign(−)

Check (c) by decoding the packed form: nibbles 0,1,2,6,5 → 01265, sign nibble 1101 → negative → −1265-1265 ✓.

Exercise 5

Encode the text IBM 7 (five characters including the space) in EBCDIC and in ASCII. Give hex and binary.

Show solution ↓
Solution

EBCDIC (zone | digit from the code table):

  • I: letters A–I use zone 1100 and digits 0001–1001; I is the 9th → 1100 1001 = C9.
  • B: 2nd letter → 1100 0010 = C2.
  • M: J–R use zone 1101; M is the 4th of that group (J, K, L, M) → 1101 0100 = D4.
  • space: 0100 0000 = 40.
  • 7: zone 1111, digit 0111 → 1111 0111 = F7.

ASCII (7-bit, shown with a leading 0 in 8 bits):

  • I: 4116+8=491641_{16} + 8 = 49_{16} → 0100 1001
  • B: 421642_{16} → 0100 0010
  • M: 4116+12=4D1641_{16} + 12 = 4\text{D}_{16} → 0100 1101
  • space: 201620_{16} → 0010 0000
  • 7: 3016+7=371630_{16} + 7 = 37_{16} → 0011 0111
CharEBCDICASCII
IC949
BC242
MD44D
space4020
7F737

The same text gives completely different bytes, which is why a file must be converted when moved between an IBM mainframe and an ASCII system.

Exercise 6

For each character give its Unicode code point, its plane, and whether it is in the BMP: (a) “A” (b) Thai “ก” (U+0E01) (c) ”€” (U+20AC) (d) ”😀” (U+1F600). Then (e) how many code points does Unicode have and how many bits are needed for the largest one?

Show solution ↓
Solution

The plane is the part of the code point in front of the last four hex digits (code point ÷216\div 2^{16}).

CharCode pointPlaneIn BMP?Block (Null & Lobur layout)
(a) AU+00410yesalphabets; equals ASCII 41
(b) กU+0E010yesalphabets (0000–1FFF)
(c) €U+20AC0yessymbols (2000–2FFF)
(d) 😀U+1F6001nosupplementary plane

For (d): 1F60016÷1000016=11\text{F}600_{16} \div 10000_{16} = 1 remainder F60016\text{F}600_{16}, so plane 1.

(e) 1717 planes × 65,536=1,114,112\times\ 65{,}536 = 1{,}114{,}112 code points (U+0000 to U+10FFFF). The largest, 10FFFF16=1 0000 1111 1111 1111 1111210\text{FFFF}_{16} = 1\,0000\,1111\,1111\,1111\,1111_2, has 21 bits, so Unicode is a 21-bit code space.

Exercise 7

A terminal receives the hex bytes 53 C9 D4 using 7-bit ASCII with even parity. Check the parity of each byte and decode the message.

Show solution ↓
Solution
HexBinaryNumber of 1sParityStrip parity bit7-bit hexChar
530101 00114✓ even101 001153S
C91100 10014✓ even100 100149I
D41101 01004✓ even101 010054T

All three bytes pass, and the message is “SIT”.

Check by re-encoding: S = 101 0011 has four 1s → parity 0 → 53 ✓; I = 100 1001 has three 1s → parity 1 → C9 ✓; T = 101 0100 has three 1s → parity 1 → D4 ✓.