Say I have a bunch of ASCII codes joined into one int value. For eg - My name "Rahul" yields 8297104117108( x =''.join(str(ord(c))for c in "Rahul"
). How do I convert this integer back to the word that is formed using the ASCII codes?
If you limit this to printable characters ( ascii >= 32 ) in an 8 bit character set (extended ASCII), there is actually no ambiguity. Each characters will use either 2 or 3 digits. Characters with two digits will be >= 32 and characters with 3 digits will be <= 255 (which happens to start with a two digit number below 32). So two consecutive digits < 32 can only be the beginning of a 3 digit character.
def decodeStr(s):
if s == "": return ""
code = s[:3] if s[:2] < "32" else s[:2]
return chr(int(code)) + decodeStr(s[len(code):])
sa = decodeStr("8297104117108")
print(sa) # "Rahul"
If you have a very long string to decode, the recursive approach might hit Python's maximum recursion limit. Here is an iterative variant of the function:
def decodeStr(s):
result = ""
code = ""
for digit in s:
code += digit
if len(code) < 2 or code < "32" : continue
result += chr(int(code))
code = ""
return result
The iterative function will run about 1.8x faster than the recursive one.
here is a simple, but not very efficient solution:
d = '8297104117108'
l = []
while d:
if d[0] == '1':
l.append(chr(int(d[:3])))
d = d[3:]
else:
l.append(chr(int(d[:2])))
d = d[2:]
print(''.join(l))
this assumes that the range of you characters is pretty limited, if you can mess with the "encryption", then pad your number to be of length 3. like so:
my_code = ''.join('{:03}'.format(ord(c))for c in "Rahul")
this will give you '082097104117108'
which you can then read 3 at a time
''.join([chr(int(my_code[i:i+3])) for i in range(0,len(my_code),3)])
An incremental solution assuming all codes are in range(65,650)
, so you can extend your text to all latin extensions :
def decode(code):
res=''
car=0
for digit in code:
car = 10*car + int(digit)
if car >= ord('A'):
res += chr(car)
car=0
return res
decode('8297104117108')
is 'Rahul'
, decode('8297250108')
is 'Raúl'
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.