您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

十六进制字符串到python中的字节数组

十六进制字符串到python中的字节数组

假设您的十六进制字符串类似于

>>> hex_string = "deadbeef"

将其转换为字符串(Python≤2.7):

>>> hex_data = hex_string.decode("hex")
>>> hex_data
"\xde\xad\xbe\xef"

或从Python 2.7和Python 3.0开始:

>>> bytes.fromhex(hex_string)  # Python ≥ 3
b'\xde\xad\xbe\xef'
>>> bytearray.fromhex(hex_string)
bytearray(b'\xde\xad\xbe\xef')

请注意,这bytes是的不变版本bytearray

python 2022/1/1 18:24:08 有452人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶