Converts a string of characters to ascii
1 2 3 4 5 6 7 8 9 10 11 12 13 | def encrypt(string):
a = string
new_string = ''
for x in a:
new_string = new_string+str(ord(x))+' '
return new_string
def unencrypt(string):
a = string
new_string = ''
b = a.split()
for x in b:
new_string = new_string+chr(int(x))
return new_string
|
To learn about character encodings check out the pyzine article:
This would be slightly faster and slightly shorter if you used list comprehensions and ''.join instead of assembling the string yourself, as in:
Faster still is removing the list comprehension and using an inline generator:
And better yet - faster by a factor of four and far more efficient space-wise - use base64 conversion for this:
@Gahl Saraf
Generator expressions are slower than list comprehensions because there's more overhead involved. List comprehensions use more memory, but are sometimes noticeably faster. Compare the speed of str.join() and tuple() using list comprehensions vs. generator expressions.