【问题标题】:How to encode a text string into a number in Python?如何在 Python 中将文本字符串编码为数字?
【发布时间】:2019-08-19 19:42:24
【问题描述】:

假设你有一个字符串:

mystring = "Welcome to the InterStar cafe, serving you since 2412!"

我正在寻找一种将字符串转换为数字的方法,例如:

encoded_string = number_encode(mystring)

print(encoded_string)

08713091353153848093820430298

..你可以转换回原来的字符串。

decoded_string = number_decode(encoded_string)

print(decoded_string)

"Welcome to the InterStar cafe, serving you since 2412!"

它不必是加密安全的,但它必须为相同的字符串输出相同的数字,而不管它在哪台计算机上运行。

【问题讨论】:

  • 你想做什么?通过网络传输字符串?如果是这样,为什么不使用base 64编码,base64很简单。
  • 好像你想先encode the string as bytes,然后decode those as an int,然后逆向把字符串取出来。但这样做并没有什么意义 - 你确定这不是XY problem?
  • 知道你为什么要这样做会真正帮助你想出最好的方法。 @RandomDavis 的建议适用于几乎所有字符串(除了以 '\0' 字符开头/结尾的字符串,具体取决于您选择的字符串),但如果您的字符集受到更多限制,您也可以获得较小的数字。或者,也许您想要固定大小的数字。 (还是先压缩字符串?)
  • 取决于您想要的输出(一对一字符映射?比字符串更长的数字字符串?)您可以使用ord() 编码并使用chr() 解码,或者您可以根据a 替换自定义字典,或任意数量的选项

标签: python python-3.x


【解决方案1】:

encode 将其转换为固定编码的bytes,然后将bytes 转换为带有int.from_bytes 的int。反向操作是在生成的int 上调用.to_bytes,然后将decode 调用回str:

mystring = "Welcome to the InterStar cafe, serving you since 2412!"
mybytes = mystring.encode('utf-8')
myint = int.from_bytes(mybytes, 'little')
print(myint)
recoveredbytes = myint.to_bytes((myint.bit_length() + 7) // 8, 'little')
recoveredstring = recoveredbytes.decode('utf-8')
print(recoveredstring)

Try it online!

这有一个缺陷,即如果字符串以NUL 字符('\0'/\x00')结尾,你会丢失它们(切换到'big' 字节顺序会从前面丢失它们)。如果这是一个问题,您总是可以明确地使用 '\x01' 填充并在解码端将其删除,这样就不会丢失尾随的 0:

mystring = "Welcome to the InterStar cafe, serving you since 2412!"
mybytes = mystring.encode('utf-8') + b'\x01'  # Pad with 1 to preserve trailing zeroes
myint = int.from_bytes(mybytes, 'little')
print(myint)
recoveredbytes = myint.to_bytes((myint.bit_length() + 7) // 8, 'little')
recoveredstring = recoveredbytes[:-1].decode('utf-8') # Strip pad before decoding
print(recoveredstring)

【讨论】:

    【解决方案2】:

    如果您只是想让某个字符串无法被人类读取,您可以使用base64、base64.b64encode(s, altchars=None) 和base64.b64decode(s, altchars=None, validate=False):

    考虑到它需要类似字节的对象,所以你应该以b"I am a bytes-like string":开始你的字符串

    >>> import base64
    >>> coded = base64.b64encode(b"Welcome to the InterStar cafe, serving you since 2412!")
    >>> print(coded)
    b'V2VsY29tZSB0byB0aGUgSW50ZXJTdGFyIGNhZmUsIHNlcnZpbmcgeW91IHNpbmNlIDI0MTIh'
    >>> print(base64.b64decode(coded))
    b"Welcome to the InterStar cafe, serving you since 2412!"
    

    如果你已经有了你的字符串,你可以用str.encode('utf-8')转换它们:

    >>> myString = "Welcome to the InterStar cafe, serving you since 2412!"
    >>> bString = myString.encode('utf-8')
    >>> print(bString)
    b'Welcome to the InterStar cafe, serving you since 2412!'
    >>> print(bString.decode())
    'Welcome to the InterStar cafe, serving you since 2412!'
    

    如果您确实需要将字符串仅转换为数字,则必须使用@ShadowRanger's answer。

    【讨论】:

    • 您假设“编码”是唯一的目标。他们可能有一些 API 需要一个真正的整数(一些玩具 RSA 加密器等),而这对此无济于事。
    猜你喜欢
    • 2016-04-05
    • 2018-06-21
    • 2023-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多