Information Technology Topic

BCD and ASCII Codes

  1. Read notes
  2. Flashcards
  3. Quiz
  4. Confident

Study focus - 3 things

  1. BCD (binary coded decimal) writes each decimal digit 0 to 9 as its own 4-bit group, so 49 is 0100 1001, which is not the same as 49 in straight binary (110001)
  2. ASCII gives every character a number code: the standard table uses 7 bits (128 patterns) and is normally stored in an 8-bit byte; A is 65, a is 97, 0 is 48 and a space is 32
  3. A digit typed as text (the character '7', code 55) is not the same as the number 7 stored in binary (00000111) or in BCD (0111)

Section 1Binary coded decimal (BCD)

Straight binary changes the whole number into base 2. Binary coded decimal does something different: it keeps the decimal digits and writes each one separately as a 4-bit binary group. Digit | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 BCD | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 Only the ten groups 0000 to 1001 are used. The groups 1010 to 1111 (six of the sixteen patterns) are not valid BCD digits. It is not the same as straight binary: 49 in binary is 110001, but in BCD it is 0100 1001.

Section 2Converting to and from BCD

Decimal to BCD: change each decimal digit on its own into four bits, and keep the digits in the same order. 264: 2 = 0010, 6 = 0110, 4 = 0100, so 264 is 0010 0110 0100. BCD to decimal: split the bits into groups of four from the left and read each group as one digit. 1001 0111 0010: 1001 = 9, 0111 = 7, 0010 = 2, so the number is 972. Here's the trick: you never add place values across the groups. Each group stands alone, and the groups are only put side by side.

Section 3Why use BCD, and what does it cost?

BCD is easy to convert to and from what people see, because each digit on a display or printout has its own group. That makes it handy for things like calculators and meter readings. The cost is space. Each digit needs 4 bits, but 4 bits could hold 16 patterns and BCD uses only 10 of them. So 972 needs 12 bits in BCD but only 10 bits in straight binary (1111001100). A byte has 8 bits, so it can hold two BCD digits (this is called packed decimal). That means one byte of BCD holds 0 to 99, while the same byte in straight binary holds 0 to 255.

Section 4Character codes and ASCII

A computer can only store numbers, so every letter, digit and symbol is given a number, its character code. ASCII is a widely used code for English text. The standard ASCII table has 128 codes, which fit in 7 bits. It is normally stored in an 8-bit byte, with the leftmost bit as 0. Because most computers handle 8 bits at a time, an 8-bit version of ASCII was also developed. The codes are in a sensible order, so you only need to remember a few anchor values and count from them. This topic uses the standard letters, digits and the space character only.

Section 5The ASCII anchor values

Space = 32 The digits '0' to '9' = 48 to 57 The capital letters 'A' to 'Z' = 65 to 90 The small letters 'a' to 'z' = 97 to 122 So 'A' is 65, 'B' is 66, 'C' is 67 and so on. 'a' is 97, 'b' is 98 and so on. The digit '5' is 48 + 5 = 53. Easy to mix up: each small letter is 32 more than its capital. 'A' (65) and 'a' (97) differ by 32, and in binary they differ by exactly one bit.

Section 6ASCII codes in binary

To write a code in binary, convert its decimal value to 8 bits using the method from the binary conversion topic. Character | A | a | space | 0 | 5 | 9 Decimal | 65 | 97 | 32 | 48 | 53 | 57 8-bit binary | 01000001 | 01100001 | 00100000 | 00110000 | 00110101 | 00111001 To decode, do the reverse: turn the 8 bits into decimal, then find the character. 01001011 is 64 + 8 + 2 + 1 = 75, which is the 11th letter after 64, so 75 is 'K'. A word is just a row of codes: 'Hi' is 72 and 105, which is 01001000 01101001.

Section 7A digit as text, as a number, and as BCD

There are three different ways to store the digit 7, and they are different bit patterns. As the number 7 in straight binary: 00000111. As a BCD digit: 0111. As the character '7' in ASCII text: 00110111, which is the code 55 (48 + 7). The text form takes a whole byte for each digit: the number 11 typed as text is two bytes (one for each '1'), but stored as a number for calculation it is one byte, 00001011. That is why a program converts typed digits into numbers before it does arithmetic.

Examples

A digital meter in a Kingston shop shows the reading 4096. The display unit stores each decimal digit in BCD. A technician also reads the BCD number 1001 0111 0010 from another meter. (a) Write 4096 in BCD. (b) Convert 1001 0111 0010 back to decimal. (c) Compare the number of bits needed to store 972 in BCD and in straight binary.

(a) Change each digit on its own: 4 = 0100, 0 = 0000, 9 = 1001, 6 = 0110. So 4096 is 0100 0000 1001 0110. (b) Read each group as a digit: 1001 = 9, 0111 = 7, 0010 = 2. The number is 972. (c) In BCD, 972 uses three groups of 4 bits, which is 12 bits. In straight binary, 972 = 512 + 256 + 128 + 64 + 8 + 4 = 1111001100, which is 10 bits. BCD uses 2 more bits because each digit gets a full 4 bits.

A label printer stores the text Jam 5 as 8-bit ASCII codes. The anchor values are: space = 32, '0' = 48, 'A' = 65 and 'a' = 97. (a) Find the decimal ASCII code of each of the five characters J, a, m, space and 5. (b) Write each code as 8-bit binary. (c) The small letter 'j' is also needed. State its code in binary and compare it with 'J'.

(a) 'J' is the 10th letter, so its code is 65 + 9 = 74. 'a' is 97. 'm' is the 13th letter, so 97 + 12 = 109. The space is 32. The digit '5' is 48 + 5 = 53. (b) 74 = 64 + 8 + 2, so 01001010. 97 = 64 + 32 + 1, so 01100001. 109 = 64 + 32 + 8 + 4 + 1, so 01101101. 32 = 00100000. 53 = 32 + 16 + 4 + 1, so 00110101. So Jam 5 is 01001010 01100001 01101101 00100000 00110101, which is 5 bytes. (c) 'j' is 97 + 9 = 106 = 64 + 32 + 8 + 2, so 01101010. Compared with 'J' (01001010), only one bit is different, the third from the left, because the codes differ by 32.

Now let's water it

Session runs notes to flashcards to quiz back-to-back.

Where this comes from