简体   繁体   English

如何将包含各种ASCII码的int值转换为相应的字符串?

[英]How to convert an int values consisting of various ASCII codes to its corresponding word string?

Say I have a bunch of ASCII codes joined into one int value. 假设我有一堆ASCII码加入一个int值。 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? 例如 - 我的名字“Rahul” x =''.join(str(ord(c))for c in "Rahul"产生8297104117108( x =''.join(str(ord(c))for c in "Rahul" )。如何将此整数转换回使用ASCII代码形成的字?

If you limit this to printable characters ( ascii >= 32 ) in an 8 bit character set (extended ASCII), there is actually no ambiguity. 如果将此值限制为8位字符集(扩展ASCII)中的可打印字符(ascii> = 32),则实际上没有歧义。 Each characters will use either 2 or 3 digits. 每个字符将使用2或3位数字。 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). 两位数的字符将> = 32,3位数的字符将<= 255(恰好以32位以下的两位数字开头)。 So two consecutive digits < 32 can only be the beginning of a 3 digit character. 因此,两个连续的数字<32只能是3位数字符的开头。

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. 如果你有一个很长的字符串要解码,递归方法可能会达到Python的最大递归限制。 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. 迭代函数的运行速度比递归函数快1.8倍。

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: 这假设您的角色范围非常有限,如果您可以搞乱“加密”,那么将您的数字填充为长度为3.如下所示:

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 这将给你'082097104117108' ,你可以一次阅读3

''.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 : 假设所有代码都在range(65,650)的增量解决方案,因此您可以将文本扩展到所有拉丁扩展:

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' . decode('8297104117108')'Rahul'decode('8297250108')'Raúl'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM