- ASCII code
ASCII stands for American Standard Code for Information Interchange. ASCII code is how characters are reperesented in the bytes of a computer's RAM or disk space. You should know that computers process everything in one's and zero's. The most basic unit in the computer is a bit. A bit
is either on (1) or off (0). Eight bits are used to make a byte. There are 256 different permutations of one's and zero's in a byte. In other words, a byte can hold a value ranging from zero (0000 0000) to 255 (1111 1111). In ASCII code the values from 0 to 127 are used to represented various characters including the digits 0-9, the uppercase alphabet, the lowercase alphabet, and punctuation. The values from 128 to 255 contain special characters and this part of the ASCII code varies between computer brands and operating systems. You can examine a portion of the ASCII code in the table below.
ASCII ASCII ASCII
code character code character code character
33 ! 40 ( 47 /
34 " 41 ) 48 0
35 # 42 * 49 1
36 $ 43 + 50 2
37 % 44 , 51 3
38 & 45 - 52 4
39 ' 46 . 53 5
In C++, a char variable is contained in a single byte.
The char() function allows you to select the value of a byte so that it displays the character you want.
For example the following code
cout << char(36)<< char(49)<< char(46)<< char(53)<< char(48);
displays
.
Refer to the values in the ASCII table above if you are not sure why.
The Do-While loop below displays the digits 0-9 in reverse order.
It starts with char(58) which holds the ASCII value for the character 9 (not the actual decimal value of 9). The -- after count is the opposite of ++. The line count--; means decrease the value of count by 1.
count = 58;
do{
cout << char(58) << ", ";
count--; // subtract 1 from count;
}while(count > 47); // tells us to stop when count reaches 47
(Note: This Do-While loop doesn't require input so no priming is needed.)
Assignment #4
1.Write a program to produce an ASCII table on-screen. Do not print the first 31 characters.
(Remember to stop at 255.)
2.Write a program that gets the users name and then prints it backward to the screen.