Reputation: 21
I am having problems with the chr() function. My code first takes the user input, puts it into an array and then uses the ord function to convert it to the corresponding number value and does the same with a keyword. It then uses the zip function to add both arrays together to get numbers and uses the chr function on it to put it back to text. The problem is that the chr function outputs incorrectly
E.g
the number value of c is 99
the number valueof h is 104
They add together to make 203
my code outputs \xcb
when I made a separate code that is just
leb = chr(203)
print leb
That outputs 203 as Ë which is a singular character and what I need for decoding it
Here is my code
num_string = raw_input("enter what you would like to encrypt")
num_list = []
for num in num_string:
num_list.append(ord(num))
print num_list
key_string = raw_input("enter the keyword")
key_list = []
for key in key_string:
key_string = key_list.append(ord(key))
print key_list
end_string = [x + y for x, y in zip(num_list, key_list)]
end_list = []
print end_string
for end in end_string:
end_list.append(chr(end))
print end_list
When this is run this is the output
enter what you would like to encrypt cat
[99]
[99, 97]
[99, 97, 116]
enter the keywordhat
[104]
[104, 97]
[104, 97, 116]
[203, 194, 232]
['\xcb', '\xc2', '\xe8']
Why is my code doing this and how do I fix this?
Upvotes: 2
Views: 3763
Reputation: 149125
All is perfectly normal. It is just the difference between the value of a string (with quotes around) and the printing of the string after converting it in the system code page. In latin1 code page, the character '\xcb'
(code 203) effectively prints as a Ë
.
But printing a list is not the same as printing its values, lets forget for characters using codes above 127, and just look at this:
>>> l = [ 'a', 'b', 'c']
>>> print l
['a', 'b', 'c']
>>> print ','.join(l)
a,b,c
So when printing a list of strings, Python prints each element in its representation form, that is enclosed in quote. So in your example, (assuming your system encoding is latin1), you can write:
...
print end_list
print ''.join(end_list)
and you will get as expected:
['\xcb', '\xc2', '\xe8']
ËÂè
Upvotes: 2
Reputation: 411
As suggested in this SO question(python: extended ASCII codes), there is no such thing as "extended Ascii".
The output is correct( \xcb = 12 * 2^4 + 11 * 2^0), but it is apparently not the encoding you are looking for, so you may want to figure out what encoding it is you are looking for and then consult the Python documentation of the built-in encode function.
Upvotes: 1