简体   繁体   English

将整数转换为其ascii值的字符串

[英]Convert an integer into a string of its ascii values

Given a number number such that its digits are grouped into parts of length n (default value of n is 3) where each group represents some ascii value, I want to convert number into a string of those ascii characters. 给定一个号码number ,使得其位数被分组为长度的部分n (默认值n为3),其中每个基团代表一些ASCII值,我想转换number到这些ASCII字符的字符串。 For example: 例如:

n                number     Output
==================================
3                    70          F
3           65066066065       ABBA
4        65006600660065       ABBA

Note that there is no leading 0 in number, so the first ascii value will not necessarily be represented with n digits. 请注意,数字中没有前导0 ,因此第一个ascii值不一定用n数字表示。

My current code looks like this: 我当前的代码如下所示:

def number_to_string(number, n=3):
    number = str(number)
    segment = []

    while number:
        segment.append(number[:n])
        number = number[n:]

    return str(''.join('{:0>{}}'.format(chr(segment), n) for segment in number))

Expected outputs: 预期产出:

number_to_string(70)
'F'

number_to_string(65066066065)
'ABBA'

number_to_string(65006600660065, n=4)
'ABBA'

My current code however returns an empty string. 然而,我当前的代码返回一个空字符串。 For example, instead of 'F' it returns ' ' . 例如,它代替'F'返回' ' Any reason why this is? 这是什么原因? Thank you! 谢谢!


PS: PS:
I'm wanting to reverse the process of this question , ie turn an integer into a string based on the ascii values of each character (number) in the string. 我想要反转这个问题的过程,即根据字符串中每个字符(数字)的ascii值将整数转换为字符串。 But reading that question is not a requirement to answer this one. 但阅读这个问题不是回答这个问题的必要条件。

Try this: 尝试这个:

import re

def number_to_string(num, n=3):
    num_str = str(num)
    if len(num_str) < n:
        num_str = '0' * (n-len(num_str)) + num_str
    elif len(num_str) % n != 0:
        num_str = '0'*(n-len(num_str)%n) + num_str
    print(num_str)

    chars = re.findall('.'*n, num_str)
    l = [chr(int(i)) for i in chars]
    return ''.join(l)

First pad the given number (converted into string) with required number of zeros, so that it can be evenly split into equal number of characters each. 首先用给定数量的零填充给定数字(转换为字符串),以便可以将它们均匀地分成相等数量的字符。 Then using re split the string into segments of size n . 然后使用re将字符串拆分为大小为n段。 Finally convert each chunk into character using chr , and then join them using join . 最后使用chr将每个块转换为chr ,然后使用join将它们join

def numToStr(inp):
"""Take a number and make a sequence of bytes in a string"""
out=""
while inp!=0:
    out=chr(inp & 255)+out
    inp=inp>>8
print "num2string:", out 
return out

does this help? 这有帮助吗?

Is this what you want? 这是你想要的吗?

def num_to_string(num, leng):
    string = ""
    for i in range(0,len(str(num)),leng):
        n = str(num)[i:i+2]
        string += chr(int(n))
    print string

Output: 输出:

>>> ================================ RESTART ================================
>>> 
>>> num_to_string(650065006600,4)
AAB
>>> num_to_string(650650660,3)
AAB
>>> num_to_string(656566,2)
AAB
>>> 

您可以将\\ x附加到数字,因为这会打印'p':

print '\x70'

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

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