cs101 lecture 03: hexadecimal numbers text · pdf file1/23/13 1 computer science cs101 lecture...
TRANSCRIPT
1/23/13
1
Computer Science
CS101 Lecture 03: Hexadecimal Numbers Text Representation
Hexadecimal Numbers Text Encoding ASCII and Unicode Aaron Stevens ([email protected]) 23 January 2013
Computer Science
What You’ll Learn Today
A great shortcut for writing binary numbers How do computers store text information?
1/23/13
2
Computer Science
Just Call Me!
Here’s my phone number: 000101101111111110010110010000011001!
What’s wrong with this number? !
– Hard to write on a napkin – Vulnerable to transcription errors – Won’t make you popular at parties
Computer Science
Binary, Hexadecimal, Decimal
Each four bits map to a hex digit. Hexadecimal prefix 0x????
No inherent value, just means “treat as a hex number”
0x94D3
1/23/13
3
Computer Science
Hexadecimal to Decimal
Convert each hex digit into 4 bits. Convert binary to decimal. Example: 0x94D3 = 1001 0100 1101 0011 = 215 + 212 + 210 + 27 + 26 + 24 + 21 +
20 = 32768 + 4096 + 1024 + 128 + 64 +
16 + 2 + 1 = 38099 (decimal)
Computer Science
Conversions Between Number Systems
Try some! http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html
My phone number: 0x16FF96419
(or:
0001 0110 1111 1111 1001 0110 0100 0001 1001)
1/23/13
4
Computer Science
Binary Representations
Recall: a single bit can be either a 0 or a 1 What if you need to represent more than 2 choices? n bits can represent 2n possible combinations
Computer Science
Representing Text
A character set is a list of characters and the numbers (binary codes) used to represent each one.
1/23/13
5
Computer Science
The ASCII Character Set
ASCII stands for American Standard Code for Information Interchange
ASCII originally used seven bits to represent each character, allowing for 128 unique characters
Later extended ASCII evolved so that all eight bits were used.
Computer Science
The ASCII Character Set (7 bits)
1/23/13
6
Computer Science
ASCII Encoding
Example: Hello, world! H -> 0x48 -> 01001000!e -> 0x65 -> 01100101!l -> 0x6c -> 01101100!l -> 0x6c -> 01101100!o -> 0x6f -> 01101111!, -> 0x2c -> 00101100! -> 0x20 -> 00100000!w -> 0x77 -> 01110111!o -> 0x6f -> 01101111!r -> 0x72 -> 01110010!l -> 0x6c -> 01101100!d -> 0x64 -> 01100100!! -> 0x21 -> 00100001
Encoding Algorithm: For each character: Find it’s ASCII code. Convert to binary.
Computer Science
ASCII Decoding
01000010 01100101 00100000 01110100 01110010 01110101 01100101 00100000 01110100 01101111 00100000 01111001 01101111 01110101 01110010 00100000 01110011 01100011 01101000 01101111 01101111 01101100!
!01000010 -> 0x42 -> B!!01100101 -> 0x65 -> e!
Decoding Algorithm: For each 8 bits: Convert Hex/decimal value Lookup ASCII symbol
1/23/13
7
Computer Science
ASCII Decoding
01000010 -> 0x42 -> B!01100101 -> 0x65 -> e!00100000 -> 0x20 -> !01110100 -> 0x74 -> t!01110010 -> 0x72 -> r!01110101 -> 0x75 -> u!01100101 -> 0x65 -> e!00100000 -> 0x20 -> !01110100 -> 0x74 -> t!01101111 -> 0x6f -> o!00100000 -> 0x20 -> !01111001 -> 0x79 -> y!01101111 -> 0x6f -> o!01110101 -> 0x75 -> u!01110010 -> 0x72 -> r!00100000 -> 0x20 -> !01110011 -> 0x73 -> s!01100011 -> 0x63 -> c!01101000 -> 0x68 -> h!01101111 -> 0x6f -> o!01101111 -> 0x6f -> o!01101100 -> 0x6c -> l!
Computer Science
The Extended ASCII Character Set
1/23/13
8
Computer Science
ASCII Art
Computer Science
Can't You Take a Joke? :-)
Carnegie Mellon professor Scott E. Fahlman Proposed ASCII emoticons, Sept. 19, 1982. Source: http://www.wired.com/science/discoveries/news/2008/09/dayintech_0919
1/23/13
9
Computer Science
Unicode Character Set
Extended ASCII is not enough for international use. Unicode uses 16 bits per character How many characters can UNICODE represent? Unicode is a superset of ASCII -- the first 256 characters
correspond exactly to the extended ASCII character set.
Computer Science Unicode Character Distribution
1/23/13
10
Computer Science
Computer Science
What You Learned Today
More encoding!
Hexadecimal Numbers Character Sets ASCII Unicode
1/23/13
11
Computer Science Announcements and To Do
Homework 1 due tonight! Readings:
Reed ch 5, pp 83-87, 89-90 (today)
Lab 2 on THURSDAY Go to your scheduled lab time. You must submit a document from the lab as evidence of
attendance!