Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
CharacterEncodingCS8:IntroductiontoComputerScience,Spring2019
Lecture#15
ZiadMatni,Ph.D.Dept.ofComputerScience,UCSB
Administrative• Hw07–DUEONTUESDAY6/4• Hw08outtoday–DUEONTHURSDAY6/6(lastdayoflecture)
• Lab06–issued– DuebynextweekThursdayby11:59PM– Alittleinvolved,sofeelfreetopair-up(optional)
• YouarestillworkingonProject#1…right?
5/30/19 Matni,CS8,Sp19 2
ReviewingYourMidterm#2Exam• Optional,butrecommendedforyoutounderstandyour
mistakes
• Ifyou’reinthe8AMlab–gotoChongLiu’sofficehours• Ifyou’reinthe9AMlab–gotoBrianYoung’sofficehours• Ifyou’reinthe10AMlab–gotoShaneMasuda’sofficehours• Ifyou’reinthe11AMlab–gotoProf.Matni’sofficehours
5/30/19 Matni,CS8,Sp19 3
FinalExamExtraReviewSessionFriday,June7th1:00–3:00PMPHELP2510
(thisisoptional)
5/30/19 Matni,CS8,Sp19 4
FinalsWeek• Dr.Matniwillhaveofficehoursonfinalsweek
Monday 1:00pm–2:30pm
5/30/19 Matni,CS8,Sp19 5
LectureOutline• ASCIICodes,UTFCodes• Functionsord()andchr()• Exercises
5/30/19 Matni,CS8,Sp19 6
UTFCodesUnicodeTransformationFormat
• ASCIIuses7bitsforitscodes– Thismeansthereare27(or126)possiblecodes– PreferredencodingforbasictextfilesintheLatinalphabet
• UTF-8isanotherstandard– Uses8bitsforitscodes(so,28=256possibles)– BackwardscompatiblewithASCII– Preferredencodingfore-mailandwebpages
• UTF-16isthe“widest”standard(uses16bits)– CapableofencodingtheentireUnicoderepertoire.
5/30/19 Matni,CS8,Sp19 8
UTF-8Schemes
5/30/19 Matni,CS8,Sp19 9
Functionschr(n)andord(c) • Charactersarestoredasnumbersincomputermemory
– Therearestandardcodesforcharacters,e.g.ASCII,UTF-8,etc…
• Forexample,'A'hascode65inASCII– Usetheordfunctiontoverifythis:ord('A')is65– Notice'A'isnotsameas'a': ord('a')is97
• Everycharacter,seen(e.g.%,!,G,=,space,tab,…)andunseen(e.g.CONTROL-X,newline…)hasanASCIIcode
5/30/19 Matni,CS8,Sp19 10
Functionschr(n)andord(c) • Likewise,youcanfindcharacterassociatedwithaparticularcodeusing
chrfunction,forexample: chr(65) is 'A'
• Youcanmanipulatenumbersinordertoprocesscharacters
chr(ord('a')+3)ischr(97),whichis'd'• Noticedigitcharactershavecodestoo!
ord('6')is545/30/19 Matni,CS8,Sp19 11
Examples• HowcanIfindoutwhat’s13lettersafter‘e’??
– Easyanswer:recitethealphabetfrom‘e’andcount13places– Codeanswer:chr(ord('e')+13),whichis'r'
• HowcanIfindoutwhat’s19lettersbefore‘Z’??– Codeanswer:chr(ord('Z')-19),whichis'G'
• What’stheASCIIcodeforthehashtagcharacter??– Codeanswer:ord('#'),whichis35
5/30/19 Matni,CS8,Sp19 12
HarderExample…• HowcanIdoan(not-found-in-Python)
“addition”of2numeralstrings,like‘3’and‘4’andget‘7’??
• Firstask:howcanImake‘3’into3? (HINT:We’llneedabaseline…)• Thatbaselineisord(‘0’) ---howfarawayintheASCIIis‘3’from‘0’???• Notethat:ord(‘3’)–ord(‘0’)=3• Sothe“addition”isdonelikethis:
(ord(‘3’)–ord(‘0’))+(ord(‘4’)–ord(‘0’))=7(anint)
or,ord(‘3’)+ord(‘4’)–2*ord(‘0’)=7
5/30/19 Matni,CS8,Sp19 13
SoICanCreateaFunctiontodoThis!defaddChars1(char1,char2):
numAddASCII=ord(char1)+ord(char2)–2*ord('0')returnnumAddASCII #Returnsaninteger
ImportantCaveat!Onlyworkswith1characternumbers!
5/30/19 Matni,CS8,Sp19 14
WhatifIWantedtoReturnaStringResult?defaddChars2(char1,char2):
numAddASCII=ord(char1)+ord(char2)–2*ord('0')charNum=chr(numAddASCII+ord('0'))returncharNum #Returnsastring
ImportantCaveat!Again,onlyworkswith1characternumbers!
5/30/19 Matni,CS8,Sp19 15
Exercise1• CreateafunctionMyCipher(myStr)–takesastringargument
• Makeseveryletterbecometheletterafterit– Letter‘a’becomes‘b’,‘b’becomes‘c’,etc…– Sothat“hello”becomes“ifmmp”(encryption)
• Relatedquestion:Howwouldyoudecryptthis?
5/30/19 Matni,CS8,Sp19 16
MyCipher()anditsReversedefMyCipher(myStr):enc_str=''forcinmyStr:enc_str+=chr(ord(c)+1)returnenc_str
defReverseMyCipher(myStr):dec_str=''forcinmyStr:dec_str+=chr(ord(c)-1)returndec_str
5/30/19 Matni,CS8,Sp19 17
Exercise2MirroredAlphabet (or“thefirstshallbethelast”)• Theletters a,b,c,d,…w,x,y,z maponto
z,y,x,w,…d,c,b,a
• Sothat “bye”becomes“ybv” and“maria”becomes“nzirz” and“abcdef”becomes“zyxwvu”
• Howwouldyoudecryptthis?• Wouldyousaythisisasymmetricencryptionscheme?5/30/19 Matni,CS8,Sp19 18
MirroredAlphabetCipher• Let’sexaminethethinkingbehindthis:
a,b,c,d,e,f,g,….,w,x,y,zmapsonto
z,y,x,w,v,u,t,…..,d,c,b,a
5/30/19 Matni,CS8,Sp19 19
OurAlgorithm1. Givenastring(message)withNnumberofletters
2. Gothrueveryletterinordertoexamineit(how?)
3. Apply“mappingformula”toeachletter
(don’tknowwhatthat“formula”isyet,butthat’sok…)
4. Onceformulaisapplied,
“gatherupthenewletters”intoaNEWstring(how?)
5. ReturnthatNEWstringastheencodedmessage
5/30/19 Matni,CS8,Sp19 20
MirrorEncrypt()defMirrorEncrypt(message):#messageisastringtyperesult=''#startwithanemptyresultforcinmessage:#gothrueveryletterinmessage#let’sapplythe“mirror”formula:nc=ord(c)nr=ord('a')+ord('z')-nc
#thenaccumulatetheencodedchars,oneatatimeresult=result+chr(nr)returnresult
5/30/19 Matni,CS8,Sp19 21
MirrorEncrypt()Questions• WhathappensifItry
MirrorEncrypt(MirrorEncrypt("cat"))?– Why?
• WhathappensifItry MirrorEncrypt("CAT")?– Why?
5/30/19 Matni,CS8,Sp19 22
YOURTO-DOsq HW7(dueonTuesday,6/4)q HW8(dueonThursday,6/6)
q Lab6(dueonThursday,6/6)q ProjectAssignment(dueonSunday,6/2)
5/30/19 Matni,CS8,Sp19 23
5/30/19 Matni,CS8,Sp19 24