简体   繁体   English

将整个 ascii 字符串转换为 int

[英]Convert whole ascii string into a int

I am looking to convert a string like "ASDF" into one number like "123456789" that can easily be turned back into "ASDF", how would I go about doing this in python 3?我正在寻找将像“ASDF”这样的字符串转换成一个像“123456789”这样可以很容易地转换回“ASDF”的数字,我 go 如何在 python 3 中执行此操作?

You can convert each character into its ascii value using ord and format them to be the same length using f"{1:02d}" and concatenate them together.您可以使用ord将每个字符转换为其 ascii 值,并使用f"{1:02d}"将它们格式化为相同的长度并将它们连接在一起。 Then when you want the string back reverse this process.然后,当您想要字符串返回时,请反转此过程。

Something like就像是

def to_num(s):
    return int("1" + "".join(map(lambda a: f"{ord(a):03d}", s)))

def to_str(n):
    num = str(n)[1:]
    chunks = [num[i:i+3] for i in range(0, len(num), 3)]
    return "".join(map(lambda a: chr(int(a)), chunks))

How about that:那个怎么样:

int.from_bytes("ASDF".encode('utf-8'), byteorder='big', signed=False)
1095976006

and back然后回来

import math
(1095976006).to_bytes(math.ceil((1095976006).bit_length() / 8), byteorder = 'big', signed=False).decode('utf-8')
'ASDF'

It uses encode/decode to get utf-8 representation of Python string as byte array and then from_bytes/to_bytes to convert it to integer. SO it works with any string.它使用编码/解码将 Python 字符串的 utf-8 表示形式作为字节数组,然后使用 from_bytes/to_bytes 将其转换为 integer。因此它适用于任何字符串。 Surprisingly, it is necessary to calculate number of bytes in order to use to_bytes.令人惊讶的是,必须计算字节数才能使用 to_bytes。

Just figured out a possible way, since I am only using ASCII this should work:只是想出了一个可能的方法,因为我只使用 ASCII,所以这应该有效:

inp = "ASDF" #Input string
bits = 8     #Amount of bits per character

num = 0      #Just some setup value, this will be our encoded number
numstr = [ord(letter) for letter in inp] #Turns the string into a list containing each of the letters ascii code
for numletter in numstr: #Loop over each letter
    num = (num<<bits)+numletter #First shift the current num stored by 8 [In binary 01010101 becomes 0101010100000000] which makes room for the next character, then adds the new character in

print("Encoded:",num) #Print the encoded number

#num = 1234, not included as we've defined it before with the encoding part
#bits = 8, same reason as above
outp = "" #Another setup value, this will be our decoded string
AND = ((2**bits)-1) #A setup constant, we'll need this later, this is a number of just 1's in binary with the length of 8.
while num>=1: #Loop over each character
     numletter = num&AND #Bitwise AND it with the constant we got earlier, this'll extract the first character in the number
     outp = chr(numletter) + outp #First convert the extracted number to it's character, then add the character to the front of the output
     num = num>>bits #Remove the extracted number and shift it back by 8

print(outp) #Print the decoded string

Kinda problematic by definition.根据定义有点问题。 Depending on the length of the string you can easily run out of ints.根据字符串的长度,您很容易用完整数。 Unless you mean number in string representation, then it's theoretically possible, but practically you will need arithmetic that works on those int as string numbers.除非你指的是字符串表示中的数字,否则理论上是可能的,但实际上你将需要将这些 int 作为字符串数字来处理的算术。 Basically, if you are restricted to ascii, use base 26 representation:基本上,如果您仅限于 ascii,请使用 base 26 表示:

"F"*26^0 + "D"*26^1 + "S" * 26^ 2 and so on

Or you can just concatenate the character codes, it's also an option, although it seems rather pointless - you don't really covert string to number, you just print its representation in another way.或者你可以只连接字符代码,这也是一个选项,虽然它看起来毫无意义 - 你并没有真正将字符串转换为数字,你只是以另一种方式打印它的表示。

Or perhaps, you are talking about mapping closed set of string to integers - then just put your strings in the list and use their index as the matching int.或者,您可能正在谈论将封闭的字符串集映射到整数 - 然后只需将您的字符串放入列表中并使用它们的索引作为匹配的 int。

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

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