Contents

In our example, each character of the String "Hello world" is represented by a number between 0 and 127. For example, to the computer, the capital letter "H" is encoded as the number 72, whereas the space is encoded as the number 32. The ASCII standard, originally developed for sending telegraphs, specifies what number is used to represent each character.

On most Unix-like operating systems, you can view the entire chart of ASCII codes by typing "man ascii" at the shell prompt. Wikipedia's page on ASCII also lists the ASCII codes. Using an ASCII chart, we discover that our string "Hello world" gets converted into the following series of ASCII codes.

H e l l o space w o r l d
72 101 108 108 111 32 119 111 114 108 100

You can also determine the ASCII code of a character by using the ? operator in Ruby 1.8.

When your Ruby program prints out the letter "H", it sends the ASCII code for "H" (72) to the terminal emulator, which then draws an "H". When your Ruby program prints out a bell character, it sends a different ASCII code – ASCII code 7 – to the terminal emulator. In this case, the terminal emulator does not draw a symbol, but instead will typically beep or flash briefly. How each of the codes gets interpreted is largely determined by the ASCII standard.