Radix Conversions, Characters Codes, Parity

Tom Kelliher, CS 220

Sept. 7, 2001

Administrivia

Announcements

Study binary, hex addition/subtraction on your own. Responsible for assigned readings.

Assignment

Read 2.1--2.

From Last Time

Outline

  1. Radix conversions.

  2. Character encodings.

  3. Parity.

Coming Up

Binary logic, gates, Boolean algebra.

From Last Time

Radix Conversion

Binary or hexadecimal to decimal is simple enough.

Decimal to binary algorithm:

/* d: decimal number
 * b: binary number
 * b[i]: bit i of b
 */

b = 0;
i = 0;

while (d != 0)
{
   b[i] = d % 2;   /* d modulo 2 (radix) */
   d = d / 2;      /* Integer division */
   ++i;
}
Example: convert to binary.

How do we modify this for hexadecimal? Repeat the example.

Character Representation

  1. So far, all we can represent is unsigned numbers. How can we represent characters?

  2. ASCII character code. A few examples using hex encodings:
    1. A: 41, a: 61.

    2. Z: 5A, z: 7A.

      Collating sequence.

    3. 0: 30, 9: 39.

    4. !: 21, =: 3D, ' ': 20.

    5. nl: 0A, cr: 0D.

    C code to convert an integer numeric string to integer value:

    char s[] = "123";
    int val;
    int i;
    
    val = 0;
    i = 0;
    
    while (isdigit(s[i])
    {
       val = val * 10;
       val = s[i] - '0';
       ++i;
    }
    

  3. ASCII is a seven-bit code; characters stored in bytes.

    What about characters for non-English languages, math characters, etc? Unicode: 16-bit character code.

Parity

  1. Used to detect data errors in memory or during simple data communications (serial lines).

    Detects single bit errors. Misses double bit errors.

  2. Other mechanisms: ECC, CRC.

  3. Idea: Maintain one extra bit which keeps total number of one bits even or odd.

    Odd parity examples: 0011010 becomes 00011010; 0110011 becomes 10110011.



Thomas P. Kelliher
Wed Sep 5 18:47:16 EDT 2001
Tom Kelliher