Python 将整个ascii字符串转换为整数

Python 将整个ascii字符串转换为整数,python,Python,我想把一个像“ASDF”这样的字符串转换成一个像“123456789”这样的数字,这个数字可以很容易地转换回“ASDF”,在python 3中我该怎么做呢?您可以使用ord将每个字符转换成ascii值,并使用f{1:02d}将它们格式化为相同的长度并将它们连接在一起。然后,当您想要返回字符串时,请反转此过程 差不多 def to_num(s): 返回int(“1”+”.join(map(lambda:f“{ord(a):03d}”,s))) def至_str(n): num=str(n)[1:]

我想把一个像“ASDF”这样的字符串转换成一个像“123456789”这样的数字,这个数字可以很容易地转换回“ASDF”,在python 3中我该怎么做呢?

您可以使用
ord
将每个字符转换成ascii值,并使用
f{1:02d}将它们格式化为相同的长度
并将它们连接在一起。然后,当您想要返回字符串时,请反转此过程

差不多

def to_num(s):
返回int(“1”+”.join(map(lambda:f“{ord(a):03d}”,s)))
def至_str(n):
num=str(n)[1:]
chunks=[num[i:i+3]表示范围内的i(0,len(num),3)]
返回“”。连接(映射(lambda:chr(int(a)),块))

您可以使用
ord
将每个字符转换为其ascii值,并使用
f“{1:02d}”
将它们格式化为相同长度,然后将它们连接在一起。然后,当您想要返回字符串时,请反转此过程

差不多

def to_num(s):
返回int(“1”+”.join(map(lambda:f“{ord(a):03d}”,s)))
def至_str(n):
num=str(n)[1:]
chunks=[num[i:i+3]表示范围内的i(0,len(num),3)]
返回“”。连接(映射(lambda:chr(int(a)),块))

从定义上讲,这有点问题。根据字符串的长度,可以很容易地用完整数。除非你们在字符串表示法中指的是数字,那个么理论上这是可能的,但实际上你们需要用算术来处理那个些整数作为字符串数字。 基本上,如果您仅限于ascii,请使用base 26表示法:

"F"*26^0 + "D"*26^1 + "S" * 26^ 2 and so on
或者,您可以将字符代码连接起来,这也是一个选项,尽管它似乎毫无意义——您并没有真正将字符串转换为数字,您只是以另一种方式打印它的表示


或者,您正在讨论将封闭字符串集映射为整数,然后将字符串放入列表中,并使用它们的索引作为匹配的整数。

从定义上讲有点问题。根据字符串的长度,可以很容易地用完整数。除非你们在字符串表示法中指的是数字,那个么理论上这是可能的,但实际上你们需要用算术来处理那个些整数作为字符串数字。 基本上,如果您仅限于ascii,请使用base 26表示法:

"F"*26^0 + "D"*26^1 + "S" * 26^ 2 and so on
或者,您可以将字符代码连接起来,这也是一个选项,尽管它似乎毫无意义——您并没有真正将字符串转换为数字,您只是以另一种方式打印它的表示


或者,您正在讨论将封闭字符串集映射为整数,然后将字符串放入列表中,并将其索引用作匹配的整数。

刚刚找到了一种可能的方法,因为我只使用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
inp=“ASDF”#输入字符串
比特数=8#每个字符的比特数
num=0#只是一些设置值,这将是我们的编码编号
numstr=[ord(字母)表示inp中的字母]#将字符串转换为包含每个字母ascii代码的列表
对于numstr中的numletter:#在每个字母上循环

num=(num刚刚找到了一种可能的方法,因为我只使用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
inp=“ASDF”#输入字符串
比特数=8#每个字符的比特数
num=0#只是一些设置值,这将是我们的编码编号
numstr=[ord(字母)表示inp中的字母]#将字符串转换为包含每个字母ascii代码的列表
对于numstr中的numletter:#在每个字母上循环
num=(num

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

import math
(1095976006).to_bytes(math.ceil((1095976006).bit_length() / 8), byteorder = 'big', signed=False).decode('utf-8')
'ASDF'
它使用encode/decode将Python字符串的utf-8表示形式转换为字节数组,然后从_bytes/转换为_bytes将其转换为整数。因此它可以处理任何字符串。令人惊讶的是,为了使用_bytes,必须计算字节数。

如何:

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

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

它使用encode/decode将Python字符串的utf-8表示形式转换为字节数组,然后从_bytes/to _bytes将其转换为整数。因此,它可以处理任何字符串。令人惊讶的是,为了使用_bytes,必须计算字节数。

检查此Python或至少Python3具有任意大的整数。您无法运行除非你的内存有问题。@RedKnite仍然可以是一个很大的数字。但是如果你使用ascii,那么base 26是很好的。啊,我忘了将字母转换成数字。所以,需要添加映射
字母={A':1,'B':2,…}
字母(“F”)*26^0+字母(“D”)*26^1+字母(“S”)*26^2
我仍然不明白转换的目的。转换的存储方式是相同的,无论您将它们传递给int-as的基数是多少。ascii的字符数不仅仅是大写字母,但如果您只处理字母,这将是一个聪明的窍门。虽然OP只使用大写字母,所以可能在这里可以:)实际上,您不需要字典
int(“ASDF”,36)
将导致463520@RedKnite我可以对你的解决方案说同样的话:并不是每个字母都可以用2或3位数字编码。想想unicode。所以,你被3块分割会在decodingPython中把事情搞砸,或者至少Python3有任意大的整数。除非你的内存有,否则你不能运行它。@RedKnite仍然可以是一个很大的数字。B如果你使用ascii码,那么以26为基数是很好的。啊,我忘了将字母转换成数字。所以,需要添加映射
字母={A':1,'B':2,…}
字母(“F”)*26^0+字母(“D”)*26^1+字母(“S”)*26^2
我仍然不明白转换的目的。转换的存储方式是相同的,无论您将它们传递给int-as的基数是多少。ascii的字符数不仅仅是大写字母,但如果您只处理字母,这将是一个聪明的窍门。虽然OP只使用大写字母,所以可能在这里可以:)实际上,您不需要字典
int(“ASDF”,36)
将导致463520@RedKnite我可以对你的解决方案说同样的话:不是每个字母都可以用2或3位数字编码。想想unicode。所以,你被3块分割会把装饰弄得一团糟