Part 1: ASCII and Character Functions

15
10/13/16 1 Unit 3: Characters, Strings, and Cryptology Computer Programming I Mr. Kindt Fall 2016 Part 1: ASCII and Character Functions

Transcript of Part 1: ASCII and Character Functions

Page 1: Part 1: ASCII and Character Functions

10/13/16

1

Unit 3: Characters, Strings, and Cryptology Computer Programming I Mr. Kindt Fall 2016

Part 1: ASCII and Character Functions

Page 2: Part 1: ASCII and Character Functions

10/13/16

2

Variable Type char

¡ A variable of type char will hold a SINGLE symbol. These symbols include: ¡  Letters: R, k, P ¡  Numbers: 4, 0, 2 ¡  These numbers cannot be

used for calculation. ¡  Symbols: &, #, ) ¡  Blank Spaces

¡ This differs from a string, as it will hold many characters.

VAR letter1, number1: char;

Problem: Computer Communication

¡  Recall: Computers process binary code through series of electrical signals.

¡  Human symbols that are input (letters, numbers) must be translated to binary and un-translated back to output.

¡  1963: 60 different computers exist worldwide, each using its own set of human symbol-binary code pairings. ¡  The letter A may have been 01000001 on one computer, but

may have been 01101100 on another.

Page 3: Part 1: ASCII and Character Functions

10/13/16

3

Solution: American Standard Code for Information Interchange (ASCII)

¡ The ASCII system assigns each character on the keyboard a whole number (ordinal).

¡ Transferring characters into numbers allows us to: ¡  Change to binary machine

language ¡  Encode messages ¡  “Shift” from upper to lower

case

¡ Most personal computers utilize this 50+ year old system today! (Exception: Unicode)

Highlights of ASCII ¡ Sequencing ¡  Capital letters begin at 65 and

progress in order ¡  Lower case letters begin at 97

¡  Shifting ¡  A lower case letter can

become an upper case letter by shifting its ordinal down 32.

¡  Result: Hitting the shift key ONLY changes one bit in the byte ¡  a = 97 = 01100001 ¡  A = 65 = 01000001

¡ Character-Integer Shift ¡  Each digit character 48-57 is 48

greater than its integer value ¡  ‘5’ has ordinal 53 ¡  53 – 48 = 5

Page 4: Part 1: ASCII and Character Functions

10/13/16

4

char functions These functions will help you work with and modify variables of type char.

¡ ORD

¡ CHR

¡  PRED

¡  SUCC

char functions: ord

¡  Returns the ASCII ordinal for a particular character. ¡  In this example, X is of type char and Y is of type longint.

X:= ‘S’; Y:= ord(X); writeln(Y); OUTPUT: 83 This is not a standalone function. You must store the result of the ord function in a longint variable.

Page 5: Part 1: ASCII and Character Functions

10/13/16

5

char functions: chr

¡  Returns the character for an integer using the ASCII chart. ¡  In this example, X is of type char and Y is of type longint.

Y:= 65; X:= chr(Y); writeln(X); OUTPUT: A This is not a standalone function. You must store the result of the char function in a chr variable.

char functions: pred

¡  Returns the predecessor (one prior on the ASCII chart) for a character. ¡  In this example, X is of type char and Z is of type char.

X:= ‘K’; Z:= pred(X); writeln(Y); OUTPUT: J This is not a standalone function. You must store the result of the pred function in a char variable.

Page 6: Part 1: ASCII and Character Functions

10/13/16

6

char functions: succ

¡  Returns the predecessor (one after on the ASCII chart) for a character. ¡  In this example, X is of type char and Z is of type char.

X:= ‘K’; Z:= succ(X); writeln(Z); OUTPUT: L This is not a standalone function. You must store the result of the succ function in a char variable.

Summary: Character Functions ord(char) = the number matching the

character in ASCII (must be in quotes). Output is an longint.

pred(char) = the predecessor (previous) character in the ASCII list (in quotes). Output is a char.

succ(char) = the successor (next) character in the ASCII list (in quotes). Output is a char.

chr(longint) = the character matching the number in ASCII (not in quotes). Output is a char.

Page 7: Part 1: ASCII and Character Functions

10/13/16

7

How to Use Ordinal Functions ¡  Like mathematical functions, you might store the

values in variables. ¡  num1 := ord(chr2);

¡  letter := chr(num5);

¡  You can also place these directly in a writeln statement. ¡  writeln(‘The ordinal of ‘, myletter, ‘ is ‘, ord(myletter));

¡  writeln(‘The character for 99 is ‘, chr(99));

Ordinal Order of Operations

¡ Simply nested; all of them occur in order from left to right considerate of nesting symbols (parentheses)

¡ chr and ord will “cancel” each other out

¡ pred and succ will also “cancel” each other out

pred('['), chr(101), succ(pred('a')), chr(ord('h')), pred(pred(pred('$'))Y e succ(''') chr(104) pred(pred(('#')Y e a h pred(' " ')

Yeah!

Page 8: Part 1: ASCII and Character Functions

10/13/16

8

Part 2: Basic Cryptology

Cryptology

¡  The process of code-making (enciphering) and code-breaking (deciphering).

¡  ENCRYPTION: The act of making plain text into ciphered text.

¡ DECRYPTION: The act of making ciphered text into plain text.

Page 9: Part 1: ASCII and Character Functions

10/13/16

9

Modern Marvels: Codes

Caesar’s Shift ¡ Adding/subtracting the same value to all

character’s ordinals to encode text.

¡ For long messages, this will be a very difficult and long code to write. There is a better way.

Encryption with ordinal shift of + 9Pascal

ord(P),ord(a),ord(s),ord(c),ord(a),ord(l)80 97 115 99 97 108

SHIFT + 989 106 124 108 106 117

chr(89),chr(106),chr(124),chr(108),chr(106),chr(117)Yj | liu

VAR L1, L2, L3, L4, L5, L6: char; BEGIN

L1:=‘P’; L2:=‘a’; L3:= ‘s’; L4:=‘c’; L5:=‘a’; L6:=‘l’;

writeln(L1, L2, L3, L4, L5, L6); L1:=chr(ord(L1)+9); L2:=chr(ord(L2)+9); L3:=chr(ord(L3)+9); L4:=chr(ord(L4)+9); L5:=chr(ord(L5)+9); L6:=chr(ord(L6)+9); writeln; writeln(L1, L2, L3, L4, L5, L6);

END.

Pascal Yj|liu

Page 10: Part 1: ASCII and Character Functions

10/13/16

10

The relationship between char and string

¡  To your computer, a string is a numbered series of char variables.

¡  Your computer is keeping track of which char is in each position of your string.

¡ Metaphor: A filing cabinet ¡  Your string is ‘Great’.

¡  The second “drawer” of your filing cabinet has been filled with an ‘r’.

¡  The fifth drawer has been filled with a ‘t’.

¡  You can print or change each char within a string!

G r

e

a t

Modifying a char from a string

¡  stringname[position] ¡  This function will return the character in the string at the position. ¡  Example:

¡  If a variable word of type string is assigned to be ‘Mularkey’, then ¡  word[4] = a ¡  word[7] = e

¡  You can print a single letter to the screen word:=‘Mularkey’; writeln(word[6]);

¡  You can use this in an ordinal shift. word:= ‘Mularkey’ word[1] := chr(ord(word[1])+7); writeln(word);

Tularkey

k

Page 11: Part 1: ASCII and Character Functions

10/13/16

11

An Encryption Example VAR

PIN, cipher: string; shift1, shift2, shift3: longint;

BEGIN write(‘Enter your three letter PIN: ’); readln(PIN); writeln; write(‘Enter encryption value 1: ‘); readln(shift1); write(‘Enter encryption value 2: ‘); readln(shift2); write(‘Enter encryption value 2: ‘); readln(shift3); writeln;

cipher := PIN; cipher[1] := chr(ord(PIN[1]) + shift1); cipher[2] := chr(ord(PIN[2]) + shift2); cipher[3] := chr(ord(PIN[3]) + shift3);

writeln(‘Your encrypted PIN: ‘, cipher);

readln; END.

Enter your three letter PIN: HAT Enter encryption value 1: 5 Enter encryption value 2: 1 Enter encryption value 1: -2 Your encrypted PIN: MBR

“String Stretch”

The “String Stretch” ¡  Every string is set up as one LARGE space of

memory.

¡  When the string is filled (through readln or assignment) the individual character compartments are CREATED and NUMBERED.

¡  If you wish to use a PREVIOUSLY UNUSED string variable to store an enciphered version of another string, you must STRETCH the string to create the compartments.

¡  This is simple: Before any enciphering, assign the new string variable to be the existing one.

¡  See the previous slide for an example of this. ¡  cipher := PIN;

¡  Failure to do so will result in terminal errors when running the .exe file.

Page 12: Part 1: ASCII and Character Functions

10/13/16

12

Part 3: String Functions

string functions These functions will help you work with and modify variables of type string.

¡  length

¡  Insert

¡  concat

¡  delete

¡  copy

Page 13: Part 1: ASCII and Character Functions

10/13/16

13

RECALL: Limiting a String ¡  If you wish to limit the maximum length of a string,

place the length in [ ] after string in the VAR section.

VAR word1: string[10];

BEGIN

word1:=‘Whole lotta stuff’; writeln(word1); readln;

END. OUTPUT: Whole lott

string functions: length

¡  Determines the number of characters in a string variable

X:= ‘Super Dooper Looper’ Y:= length(x); writeln(Y); OUTPUT: 19 //remember that this includes the spaces! This is not a standalone function. You must store the result of the length function in a longint variable.

Page 14: Part 1: ASCII and Character Functions

10/13/16

14

string functions: insert

¡  Inserts a string anywhere into another string.

¡  Insert(providing string, receiving string, position in receiving string)

K := ‘Hello ‘; K := ‘Hello ‘; L := ‘Stan’; L := ‘Stan’; Insert(L, K, 7); Insert(K, L, 3); writeln(K); writeln(L);

OUTPUT: Hello Stan OUTPUT: StaHellon

¡  This is a standalone function. You do not store the result of Insert in a variable.

string functions: concat

¡ Concatenates (puts end to end) two strings.

¡  concat(left string, right string)

K := ‘Hello ‘; L := ‘Stan’; M := ConCat(K, L); Writeln(M); OUTPUT: Hello Stan

¡  This is not a standalone function. You must store the result of the concat function in a string variable (in this example, M).

Page 15: Part 1: ASCII and Character Functions

10/13/16

15

string functions: delete

¡ Deletes a defined number of characters from a string.

¡ Delete(string, position, # of characters to delete) K := ‘NO WAY‘; K := ‘NO WAY‘; Delete(K, 2, 3); Delete(K, 3, 1); writeln(K); writeln(K);

OUTPUT: NAY OUTPUT: NOWAY

¡  This is a standalone function. You do not store the result of delete in a variable.

string functions: copy

¡ Copies a defined number of characters from a string.

¡ Copy(string, position, # of characters to copy) K := ‘NO WAY‘; K := ‘NO WAY‘; L := Copy(K, 2, 3); L := Copy(K, 5, 2); writeln(L); writeln(L);

OUTPUT: O W OUTPUT: AY

¡  This is not a standalone function. You must store the result of the copy function in a string variable (in this example, L).