如何在Python中将文本字符串编码为数字?

时间:2019-03-28 22:20:33

标签: python python-3.x

假设您有一个字符串:

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!"

它不必是密码安全的,但无论运行在什么计算机上,它都必须为相同的字符串输入相同的数字。

2 个答案:

答案 0 :(得分:2)

encode以固定编码转换为bytes,然后用bytesint转换为int.from_bytes。反向操作是在生成的.to_bytes上调用int,然后将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)

答案 1 :(得分:0)

如果您只是想让某个人无法理解的特定字符串,则可以使用base64base64.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