Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将ASCII字符串编码数组转换为字符串?_Python_Python 3.x - Fatal编程技术网

Python 将ASCII字符串编码数组转换为字符串?

Python 将ASCII字符串编码数组转换为字符串?,python,python-3.x,Python,Python 3.x,输入: '0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21' Hello! 输出: '0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21' Hello! 当前解决方案: s = [] birary_data = '0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21'.replace(' ', '').split('0x') for c in birary_data: i

输入:

'0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21'
Hello!
输出:

'0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21'
Hello!
当前解决方案:

    s = []
    birary_data = '0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21'.replace(' ', '').split('0x')
    for c in birary_data:
        if len(c) > 1:
            s.append(bytes.fromhex(c).decode('utf-8', 'ignore'))
    print("".join(s))
data = '0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21'
string = "".join([chr(int(item, 16)) for item in data.split()])
print(string)
Hello!
需要以下方面的帮助:

    s = []
    birary_data = '0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21'.replace(' ', '').split('0x')
    for c in birary_data:
        if len(c) > 1:
            s.append(bytes.fromhex(c).decode('utf-8', 'ignore'))
    print("".join(s))
data = '0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21'
string = "".join([chr(int(item, 16)) for item in data.split()])
print(string)
Hello!
有人能推荐一个更优雅的解决方案吗?

birrary\u data='0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21'。替换('0x','').split()
打印(bytearray.fromhex(“”.join(如果len(c)>1,则c代表双进制数据中的c))。解码('utf-8','ignore'))
输出:

Hello!

试试这个:

    s = []
    birary_data = '0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21'.replace(' ', '').split('0x')
    for c in birary_data:
        if len(c) > 1:
            s.append(bytes.fromhex(c).decode('utf-8', 'ignore'))
    print("".join(s))
data = '0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21'
string = "".join([chr(int(item, 16)) for item in data.split()])
print(string)
Hello!
输出:

    s = []
    birary_data = '0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21'.replace(' ', '').split('0x')
    for c in birary_data:
        if len(c) > 1:
            s.append(bytes.fromhex(c).decode('utf-8', 'ignore'))
    print("".join(s))
data = '0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21'
string = "".join([chr(int(item, 16)) for item in data.split()])
print(string)
Hello!
你可以使用下面的一个 在这里的代码中,我首先根据空格分割十六进制,然后迭代并连接我得到的字符

a = '0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21'
print(''.join(chr(int(i, 16)) for i in a.split()))

另一个选项是删除3个字符(或更少)长度的子字符串、
0x
和空格<代码>字节。fromhex可以处理类似于
'48656C6F8E21'的字符串

binary_data = '0X0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21'
binary_data = re.sub(r'\b\w{3}\b|\s?0x', '', binary_data)
print(bytes.fromhex(binary_data).decode('utf-8', 'ignore'))

内置的
bytes.fromhex()
几乎就是我们所需要的。然而,我们需要解决两个问题:

  • 前面的空字节
  • 位置6中的无效字符(
    0x8E
重新导入
数据='0x0 0x48 0x65 0x6c 0x6c 0x6f 0x8E 0x21'
string=bytes.fromhex(re.sub('0x(0)'),'',data)).decode('utf-8','ignore')

正则表达式将负责剥离空字节和正确格式化
字节的字符串。fromhex()
。解码中的
忽略
将跳过坏字节。

为什么您认为您的解决方案不优雅?@MauriceMeyer,感觉我在做额外和不必要的步骤。但是,可能有一个非常简单的解决方案,比如双进制数据。decode('hex')我总是忘记int可以取一个基数,这是一个很好的解决方案。这不是在最后一个字符之前有一个前导的空字节和其他东西吗?为什么不将您的模式更改为
r'\b\w{3}\b\s?0x'
,直接获得
'48656c6c6c6f8e21'