Humanities Home page   Office of Digital Humanities
Back     BYU LiveCode Lessons Gateway

DigHT 310
In-class Exercise:
Exploring Character Encoding


Introduction

In computers all information is ultimately stored and processed as numbers. This means that text characters must also be stored as numbers. The most commonly used system for representing the Latin alphabet on computers is called ASCII—the American Standard Code for Information Interchange—although there are several variations of this standard. The first part of this exercise is designed to help you become familiar with this standard and to introduce you to tools that LiveCode provides for accessing the underlying codes for text characters.

The ASCII Character Chart.

  1. Create a stack, name it "TextEncoding", and save it.

  2. Create a field with a vertical scrollbar named "ascii" and a button named "showascii".

  3. Create the following mouseUp handler for the button:
  4. on mouseUp
      put empty into fld "ascii"
      repeat with i = 0 to 127
        put numToChar(i) into line i+1 of fld "ascii"
      end repeat
    end mouseUp

  5. Run the script. What do you think has been put into the field?

     

  6. Let's create some line numbers to correspond to our characters. Change the 'put' statement inside the repeat loop to add a line number and tab before each line:
  7. put i & tab & numToChar(i) into line i+1 of fld "ascii"

  8. Run the script. The numbers at the beginning of each line are the decimal (base 10) codes for the first 128 characters in the ASCII character set--so called "7-bit ASCII". The characters in the first part of the list may look a bit odd (if they appear at all.) At about which ASCII code do you start to recognize common printed characters?

    Code number:
    Character:

    While it is easy for us humans to think in base 10 (after all, we have 10 fingers!), computers like base 2--ones and zeros. For programmers it is more efficient to express numbers in base 16--the digits 0 through F--also called hexdecimal. Let's add columns that show the codes in these bases. The way to change the base of a number in LiveCode is with the baseConvert() function:

      syntax: baseConvert(number to convert,from base,to base)

    So to convert the number 27 from base 10 to base 16 you would do this:

    put baseConvert(27,10,16) into myVar

    Change the statement inside the repeat loop to add a column for the hexidecimal codes for each letter:

    put i & tab & numToChar(i) & tab & baseConvert(i,10,16) into line i+1 of fld "ascii"

    How would you change the statement to add the base 2 code for each character? Try it in your button.

  9. Change your mouseUp handler to show all 256 characters in your computer's ASCII character set (8-bit ASCII).

  10. Select fld "ascii" and open its property inspector. Select the "Table" panel in the popup list. In the tab stops field enter some tab stop values (in pixels) to spread the tab-separated values in your field into neat columns. (Suggested starting values: 40,80,120. After you enter the values, hit return.) Click on the Vertical grid box to see how the columns line up.

    What is strange about the columns on line 9? Why might this have happened?
    (To help answer this question enter put charToNum(tab) in the message box.)

    What is strange about line 10? Why does nothing appear after the line number? (Hint: charToNum (LF))

  11. What problems does an 8-bit character set create?

You are not required to turn this stack in, but you should be very familiar with the information and scripting used in this exercise.


Back     BYU LiveCode Lessons Gateway
Maintained by Devin Asay.
Copyright © 2005 Brigham Young University