lecture8a

lecture8a - Monday, Nove be 1 mr C ode TheAS I I C Arrays,...

This preview shows pages 1–13. Sign up to view the full content.

Monday, November 1 st The ASCII Code Arrays, arrays, arrays… The midterm is NEXT Wednesday ! Be prepared to write code on your midterm!

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ASCII Question : How do two spies transmit a message over a telegraph? Answer : They have to encode the message as a series of short and long pulses which can travel over the wire. H = . ... e = . l = .-. . o = --- .... . He . l . . l o Agent 7 “Hello 8” Agent 8 8 = ---. . . 8 They can’t send the shape of an H over the wire, so they send pulses instead.
ASCII Instead of using dots and dashes, modern computers send and store messages by encoding them into series of 1 s and 0 s. H = 01001000 e = 01100101 l = 01101100 o = 01101111 0100 1000 0110 0101 01101100 01101111 Agent 7 “Hello 8” 8 = 00111000 00111000 Agent 8 H e l o 8 (Humans are bad at reading these “binary” numbers, so we tend to use decimal numbers instead. But the idea’s the same.)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ASCII This encoding is called the “ASCII” code. Computers represent letters, punctuation and digit symbols using this code, basically storing each character as a number . When you type a character on the keyboard, it’s converted into a number and stored in the computer’s memory! 65 50
The ASCII Code So if you could create your own encoding, how would it work? What number would you use to represent A ? How about for little a ? What about for a # sign? Or a & sign? How would you represent the digits 0 , 1 2 9 ?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Characters and ASCII Every symbol on the keyboard: letter , digit period space , etc. is represented by a numeric value in memory ! ‘A’ = 65, ‘B’ = 66, ..., ‘Z’ = 90 ‘a’ = 97, ‘b’ = 98, . .., ‘z’ = 122 ‘ ‘ = 32 (space), ‘!’ = 33, ‘,’ = 44, etc. .. ‘0’ = 48, ‘1’ = 49, ‘2’ = 50,. .. ‘9’ = 57 Well, a long time ago a bunch of computer geeks got together and made up an encoding: the ASCII Code.
The ASCII Chart: 48-63 65 97 Here’s a more complete table:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Characters and ASCII main() { char c; cin >> c; } When you type in a character like ‘A’, the computer converts it into a number and stores the number in memory. Enter a char: A Your char: A c 65 When you print a char variable out, the computer converts it back to a symbol .
Characters and ASCII main() { char c; cin >> c; } Enter a char: B Your char: B c 66 Can you guess what number will be used to represent the letter ‘ B ’?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
main() { char c1 = ‘C’ ; char c2 = 67 ; cout << c1 << endl; cout << c2 << endl; } Characters and ASCII c1 67 When using char variables in C++, you may use symbolic characters or ASCII (numeric) values. c2 C C ‘A’ = 65 ‘B’ = 66 ‘C’ = 67 ... ‘Z’ = 90 When you use cout to print a char variable , converts the number back to a symbol for display on the screen.
main() { char c1 = ‘!’ ; char c2 = 33 ; cout << c1 << endl; cout << c2 << endl; } Characters and ASCII c1 33 c2 ! ! ‘ ’ = 32 ‘!’ = 33 ... ‘/’ = 47 When using char variables in C++, you may use symbolic characters or ASCII (numeric) values.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Characters and ASCII void main(void) { char ch; cin >> ch; cout << ch << endl; cout<< static_cast<int>( ch ) ++ch; // treat it just like a number!
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/11/2011 for the course CS 33 taught by Professor Rohr during the Spring '08 term at UCLA.

Page1 / 48

lecture8a - Monday, Nove be 1 mr C ode TheAS I I C Arrays,...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online