Chapter 13

Computer Memory: Text

Recall from Chapters 5 and 9 that both integers and floating-point numbers are stored in binary, although the exact format for each is different. Binary is used because computer memory is essentially a sequence of on/off switches, and these can easily be thought of as 0’s and 1’s.

This raises the question: how is text stored in memory? The basic idea is simply to assign a code number to each character. Then, for each character, we just store the corresponding code number in binary.

The standard coding system for the English alphabet is ASCII, pronounced “ask-ee.” Standard ASCII codes are seven bits long, although each character usually occupies eight bits because that makes a complete byte. There has been disagreement about how to use that last eighth bit; however, more recently, Unicode is becoming the standard for international communication. It is a two-byte code that extends ASCII.

One of the features of ASCII is that digits and letters appear sequentially:

0–9

Codes 48–57

A–Z

Codes 65–90

a–z

Codes 97–122

Python offers two built-in functions that allow you to work directly with ASCII codes:

chr(n)

Character with ASCII code n.

ord(c)

ASCII code for the character c.

Exercises

  1. 13.1 Give the range of codes available for seven-bit ASCII if stored as unsigned integers.
  2. 13.2 Give the range of codes available for eight-bit extended versions of ASCII.
  3. 13.3 Look at binary representations of the ASCII codes for several corresponding pairs of upper and lowercase letters (for example, “E” and “e”). Find the pattern, and then use it to explain why the lowercase group does not immediately follow the uppercase group.
  4. 13.4 Explain why curly brackets {} occur at the end of the list of symbols in the index of this text. Where would they be listed relative to alphabetic characters (a–z and A–Z) if the symbols were not listed separately?
  5. 13.5 Write a program ascii.py to display an ASCII table for the characters with codes between 32 and 126.
  6. 13.6 Write the Python function encode(msg) that returns a string containing the ASCII codes of each character in msg. For example, encode("ABC") should return the string "65 66 67".
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.87.196