成功将十六进制编码为字节,但未能将字节转换回字符串-===>十六进制->字节;和字节>字符串



给定十六进制和形成字节

查找要字符串的字节


def hex_to_b:
    return bytes.fromhex('abc')
#now you have a byte type string
#looking to decode back to the string
#failure of transformation
hex_to_b().decode(encoding="utf-8",errors="strict")

要将字节转换为十六进制,反之亦然,请使用内置的binascii模块。

https://docs.python.org/3/library/binascii.html

例:

>>> from binascii import hexlify, unhexlify
>>> unhexlify('deadbeef')
b'xdexadxbexef'
>>> hexlify(b'xdexadxbexef').decode()
'deadbeef'

确保传递了有效的十六进制字符串。

最新更新