从以十六进制编码的ASCII字符串转换为纯ASCII?

Pau*_*ers 134 python hex ascii

如何在Python中从十六进制转换为纯ASCII?

请注意,例如,我想将"0x7061756c"转换为"paul".

cjm*_*cjm 219

一个稍微简单的解决方案:

>>> "7061756c".decode("hex")
'paul'
Run Code Online (Sandbox Code Playgroud)

  • Python 3上没有`.decode('hex')`.[`.decode('hex')`在Python 2上使用`binascii.unhexlify()`(http://hg.python.org/cpython /file/2.7/Lib/encodings/hex_codec.py#l27). (121认同)
  • `codecs.decode("7061756c","六角")`工程的Python 2和Python 3,但它返回一个`字节()`字符串在Python 3但那是合理的ASCII字符串. (25认同)
  • 感谢您指出这一点,据我所知,我对Python 3并不熟悉。 (2认同)

小智 86

无需导入任何库:

>>> bytearray.fromhex("7061756c").decode()
'paul'
Run Code Online (Sandbox Code Playgroud)

  • 对我来说最好的解决方案(适用于python 3),因为它甚至可以接受空格:`bytearray.fromhex(“ 70 61 75 6C”)。decode()` (2认同)

and*_*oke 43

>>> txt = '7061756c'
>>> ''.join([chr(int(''.join(c), 16)) for c in zip(txt[0::2],txt[1::2])])
'paul'                                                                          
Run Code Online (Sandbox Code Playgroud)

我只是玩得开心,但重要的部分是:

>>> int('0a',16)         # parse hex
10
>>> ''.join(['a', 'b'])  # join characters
'ab'
>>> 'abcd'[0::2]         # alternates
'ac'
>>> zip('abc', '123')    # pair up
[('a', '1'), ('b', '2'), ('c', '3')]        
>>> chr(32)              # ascii to character
' '
Run Code Online (Sandbox Code Playgroud)

现在看看binascii ......

>>> print binascii.unhexlify('7061756c')
paul
Run Code Online (Sandbox Code Playgroud)

很酷(我不知道为什么其他人想让你在他们帮助之前跳过篮球).


Jul*_*ien 9

在Python 2中:

>>> "7061756c".decode("hex")
'paul'
Run Code Online (Sandbox Code Playgroud)

在Python 3中:

>>> bytes.fromhex('7061756c').decode('utf-8')
'paul'
Run Code Online (Sandbox Code Playgroud)


car*_*era 5

这是使用十六进制整数而不是十六进制字符串时的解决方案:

def convert_hex_to_ascii(h):
    chars_in_reverse = []
    while h != 0x0:
        chars_in_reverse.append(chr(h & 0xFF))
        h = h >> 8

    chars_in_reverse.reverse()
    return ''.join(chars_in_reverse)

print convert_hex_to_ascii(0x7061756c)
Run Code Online (Sandbox Code Playgroud)


Vic*_*tes 5

在 Python 3.3.2 中测试 有很多方法可以实现这一点,这是最短的方法之一,仅使用 python 提供的东西:

import base64
hex_data ='57696C6C20796F7520636F6E76657274207468697320484558205468696E6720696E746F20415343494920666F72206D653F2E202E202E202E506C656565656173652E2E2E212121'
ascii_string = str(base64.b16decode(hex_data))[2:-1]
print (ascii_string)
Run Code Online (Sandbox Code Playgroud)

当然,如果您不想导入任何内容,您可以随时编写自己的代码。像这样非常基本的东西:

ascii_string = ''
x = 0
y = 2
l = len(hex_data)
while y <= l:
    ascii_string += chr(int(hex_data[x:y], 16))
    x += 2
    y += 2
print (ascii_string)
Run Code Online (Sandbox Code Playgroud)


小智 5

或者,您也可以这样做...

Python 2 解释器

print "\x70 \x61 \x75 \x6c"
Run Code Online (Sandbox Code Playgroud)

例子

user@linux:~# python
Python 2.7.14+ (default, Mar 13 2018, 15:23:44) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> print "\x70 \x61 \x75 \x6c"
p a u l
>>> exit()
user@linux:~# 
Run Code Online (Sandbox Code Playgroud)

或者

Python 2 单行代码

python -c 'print "\x70 \x61 \x75 \x6c"'
Run Code Online (Sandbox Code Playgroud)

例子

user@linux:~# python -c 'print "\x70 \x61 \x75 \x6c"'
p a u l
user@linux:~# 
Run Code Online (Sandbox Code Playgroud)

Python 3 解释器

user@linux:~$ python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> print("\x70 \x61 \x75 \x6c")
p a u l

>>> print("\x70\x61\x75\x6c")
paul
Run Code Online (Sandbox Code Playgroud)

Python 3 单行代码

python -c 'print("\x70 \x61 \x75 \x6c")'
Run Code Online (Sandbox Code Playgroud)

例子

user@linux:~$ python -c 'print("\x70 \x61 \x75 \x6c")'
p a u l

user@linux:~$ python -c 'print("\x70\x61\x75\x6c")'
paul
Run Code Online (Sandbox Code Playgroud)

  • 这在没有空格的情况下也可以正常工作,并且在 python3 中使用 print() 也可以正常工作。 (2认同)

小智 5

b''.fromhex('7061756c')
Run Code Online (Sandbox Code Playgroud)

不带分隔符使用