intro to computer science lab
Colorful Characters | 23 © Dmitriy Shironosov/ShutterStock, Inc.
LABORATORY
■■ Learn■how■colors■and■text■are■represented.
■Software■needed: ■ 1)■ Apps■from■the■Lab■Manual■website:■ a)■ Character■Codes■(Character■Codes.jar) b)■ Text■Translator■into■ASCII■(Text■Encoding■in■ASCII.jar) c)■ Color■Maker■(Color■Maker.jar)
4
OBJECTIVE
REFERENCES
Colorful Characters
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
24 | Laboratory 4
BACKGROUND Review these topics from your textbook, lecture notes, or online resources:
■ Representing text, the ASCII character set ■ Representing images and graphics, RGB color values
ACTIVITY ❯ Part 1
As you’ve learned, every kind of data in a computer must be represented by a sequence of ones and zeros. Computer scientists and engineers had to be enormously clever in representing the myriad data forms using only 1s and 0s: text, numbers, sounds, colors, still pictures, moving pictures, smells, . . . Wait! Smells? No, not yet (perhaps never!).
Now that we know how numbers of all kinds are represented using 1s and 0s, the next step is to learn how characters are represented. A character “mapping” assigns a number to each character, and then this assigned number is encoded in binary. For example, a very simple mapping scheme might assign 1 to A, 2 to B, and so on. A computer would then store B as 10, the binary representation of 2. It is as simple as that—except for making everyone agree on which mapping to use!
Pitched battles have been fought over character mappings. Well, that is a bit of an exaggeration, but it did take a long time for the computer industry to stop using many different mappings. IBM mainframes used EBCDIC, while Control Data Corporation (CDC) supercomputers used their own system. Digital Equipment Corporation (DEC) and other smaller companies used ASCII. Perhaps the big breakthrough came when the IBM PC was announced in 1981. Though it was an IBM, it used ASCII, and ASCII has grown ever since, to the point that it is practically the only character mapping used nowadays.
Except for Unicode, that is. With the advent of Java and web browsers and the growing international community of computer users, the Unicode character mapping has enabled us to encode virtually every written symbol as a 16-bit number. Not all software uses Unicode, and not every PC or computer can support Tibetan or Chinese script, but the mapping is in place, supported by international standards organizations. To learn more about Unicode, visit the Unicode Consortium website at http://www.unicode.org.
The Character Codes app provides a way to type in a number, using either decimal or hexadecimal, and see the corresponding character. Following is a screenshot:
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
Colorful Characters | 25
After typing “192” in the numeric code text area, press Return and you will see À, which is the letter A with a grave accent. You could also have typed “0xC0,” which is the hexadecimal number for 192. (The 0x in front is a prefix designating that the following number is base 16, or hexadecimal. x is often an abbreviation for hexadecimal and is seen in programming languages like C.)
Try the example buttons. Example 3 cycles through all characters from 33 up to 255. If your computer can’t represent a certain character, Java substitutes a little square.
❯ Part 2 The Text Translator into ASCII app makes it easy to type in some text and have the computer translate all of it to binary using the ASCII character mapping.
Start the app. Notice the character mapping in the tall text area on the left. It starts at 32, which just happens to be the blank character, and ends at 127. The codes that are mapped into the numbers 0 through 31 and 127 on up are unprintable. They have names and some are used in various functions, but in general they do not produce any graphical image on the screen.
Inside the computer’s memory, there are no spaces between the seven-bit chunks that represent each character. The computer just “knows” how wide a character is and counts off bits accordingly.
Most computers today store one ASCII character in a byte, which is eight bits, not seven. The ASCII character set was extended to include 128 extra characters, many
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
26 | Laboratory 4
of them for PC graphics. Then Unicode came along and redefined it all again. But the 128 codes with numeric values from 0 to 127 have remained stable.
You can look up tables for all 128 ASCII characters online; just search for “ASCII table” in your favorite search engine and inspect a table you find. Notice that one of the characters, number 8, is called BEL. Can you guess what it does? (It sounds a beep on the computer.) Many of the others were used in early telecommunications equipment. For example, STX is “Start of text” and ACK is “Acknowledge.”
❯ Part 3 The Color Maker app shows you two ways that colors can be encoded using 1s and 0s. The first method is called “RGB” because it represents colors as combinations of three intensity values of the colors red, green, and blue. The second method is called “HSB” because it represents colors as combinations of three values on the scales of hue, saturation, and brightness.
To begin, start the Color Maker app, and select Red from the color name choice pull- down menu in the bottom-left corner. The app sets the scrollbars and text areas to the values according to the RGB encoding and the HSB encoding.
Feel free to play with the sliders and select other colors. Notice how the colors change in the bar near the bottom of the screen. The name of the color choice in the menu does not change, however, when you change the color above by means of the sliders or by typing values into the text areas.
RGB represents the red, green, and blue values as integers from 0 to 255. Since 255 is the largest number that can be represented in eight bits, it follows that RGB requires 24 bits (8 + 8 + 8) for one given color. No wonder they call it “24-bit color”! Since 224 is 16,777,216, there are nearly 17 million different colors that can be represented in 24 bits. There is nothing magical about using eight bits except that it matches the size of a byte, the fundamental unit of computer memory in most machines nowadays.
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
Colorful Characters | 27
HSB represents the hue, saturation, and brightness as real numbers between 0 and 1.0. Leave the saturation and brightness values at 1.0 and slide the hue value up and down. Notice how the color goes from red to yellow to green to blue to magenta. The saturation number determines how much white is mixed in, and the brightness determines how strong the intensity is. If it is low, then it is like seeing the color in very dim light or a dark room.
Compare the colors red and pink by pulling down each name choice and looking at the values. In HSB, the hue value stays the same, but the saturation goes from 1.0 to about 0.31. However, the RGB version of pink shows that mixing in more green and blue with a lot of red creates pink, too. Neither RGB nor HSB is the “correct” way of viewing colors; they are just alternate ways. However, most computers store colors in RGB and convert to HSB when needed.
In summary, there are many different ways of encoding the same information in 1s and 0s. There are many different character mappings, several different ways of storing colors, and multiple ways of representing audio and video.
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
Colorful Characters | 33
DELIVERABLES Turn in your handwritten sheets showing your answers to the exercises. Also turn in one screenshot for the Text Translator into ASCII app and another screenshot for the Color Maker app.
DEEPER INVESTIGATION The CDC character set started with A = 1, B = 2, C = 3, and so forth. If you were creating a character mapping, this is probably what you would do. However, the CDC character set didn’t have lowercase letters like a, b, or c. (The original character set used six bits, so there weren’t enough possible number combinations.)
ASCII and EBCDIC do not use A = 1, or even a = 1. However, there are regularities in the character coding scheme. You discovered one such regularity in the previous exercises. Are there any others in ASCII that you can find?
Try to find a listing of EBCDIC’s codes, most likely online. You will find some surprises. For example, A = 193, B = 194, C = 195, . . . and I = 201. But J = 209. What?!?!? There are no similar breaks in ASCII. There was a reason for the breaks in EBCDIC, but you may have to dig to find out what it is. (Hint: If you get stuck, punch the keys of your computer, but not too hard!)
Switching to a different train of thought, think about how arbitrary encodings tend to be. Suppose we wanted to encode the four primary directions of the compass. Would we choose north = 1, south = 2, east = 3, and west = 4? Are there any particular reasons for choosing other mappings? (Hint: Think about telling a robot to turn completely around, smoothly, without changing directions.) Also, have we shown ethnocentrism by setting north to 1? (Have you ever seen a map of the world where the South Pole is at the top? Australian restaurants love to pin these on the wall to test the sobriety of their customers.)
Write down your views on why computer scientists veer away from completely arbitrary encodings, based on all that you have learned in this lab.
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION