如何在 Python 3 中将 PNG 数据的 hexdump 字符串转换为二进制?
How to convert hexdump string of PNG data to binary in Python 3?
我有一个小 PNG 文件的 hexdump 字符串,我想将其转换为二进制文件以写出 PNG 文件:
clip = "0x89 0x50 0x4e 0x47 0xd 0xa 0x1a 0xa 0x0 0x0 0x0 0xd 0x49 0x48 0x44 0x52 0x0 0x0 0x0 0x8 0x0 0x0 0x0 0x7 0x8 0x2 0x0 0x0 0x0 0xba 0x3b 0x9b 0x9 0x0 0x0 0x0 0x3 0x73 0x42 0x49 0x54 0x8 0x8 0x8 0xdb 0xe1 0x4f 0xe0 0x0 0x0 0x0 0x29 0x49 0x44 0x41 0x54 0x8 0x5b 0x63 0xfc 0xff 0xff 0x3f 0x3 0x36 0xc0 0x84 0x4d 0x10 0x24 0x46 0x2b 0x89 0xf7 0x1f 0xfe 0xfd 0xfc 0x9 0x75 0xb 0x8a 0x1d 0x39 0x95 0x9f 0xef 0x3e 0xfc 0xb 0x71 0xe 0x0 0xfc 0x18 0xd 0x6c 0xa9 0xf7 0x56 0x2d 0x0 0x0 0x0 0x0 0x49 0x45 0x4e 0x44 0xae 0x42 0x60 0x82 "
我尝试使用 clip.encode("hex")
但出现错误
'hex' is not a text encoding
使用 binascii.unhexlify(clip)
,我得到
Odd-length string
如果我删除空格,错误是
Non-hexadecimal digit found
None 关于 SO 或更广泛的互联网的其他讨论正在提供帮助。
您可以使用 python 的内置 eval
函数将字符串中的每个元素计算为整数。
然后在 python2.7 中可以使用内置的 chr
将整数转换为字节:
在 python3 中,您可以使用内置 bytes
转换
>>> clip = clip[:-1] # Remove last space in your clip-string
>>> hex_list = clip.split(' ') # Convert your string to list of hex-characters
# FOR PYTHON3:
>>> byte_string = bytes([eval(h) for h in hex_list])
# FOR PYTHON2.7:
>>> byte_list = [chr(eval(h)) for h in hex_list]
>>> byte_string = ''.join(byte_list) # Convert to string
>>> byte_string
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x08\x00\x00\x00\x07\x08\x02\x00\x00\x00\xba;\x9b\t\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00)IDAT\x08[c\xfc\xff\xff?\x036\xc0\x84M\x10$F+\x89\xf7\x1f\xfe\xfd\xfc\tu\x0b\x8a\x1d9\x95\x9f\xef>\xfc\x0bq\x0e\x00\xfc\x18\rl\xa9\xf7V-\x00\x00\x00\x00IEND\xaeB`\x82'
我有一个小 PNG 文件的 hexdump 字符串,我想将其转换为二进制文件以写出 PNG 文件:
clip = "0x89 0x50 0x4e 0x47 0xd 0xa 0x1a 0xa 0x0 0x0 0x0 0xd 0x49 0x48 0x44 0x52 0x0 0x0 0x0 0x8 0x0 0x0 0x0 0x7 0x8 0x2 0x0 0x0 0x0 0xba 0x3b 0x9b 0x9 0x0 0x0 0x0 0x3 0x73 0x42 0x49 0x54 0x8 0x8 0x8 0xdb 0xe1 0x4f 0xe0 0x0 0x0 0x0 0x29 0x49 0x44 0x41 0x54 0x8 0x5b 0x63 0xfc 0xff 0xff 0x3f 0x3 0x36 0xc0 0x84 0x4d 0x10 0x24 0x46 0x2b 0x89 0xf7 0x1f 0xfe 0xfd 0xfc 0x9 0x75 0xb 0x8a 0x1d 0x39 0x95 0x9f 0xef 0x3e 0xfc 0xb 0x71 0xe 0x0 0xfc 0x18 0xd 0x6c 0xa9 0xf7 0x56 0x2d 0x0 0x0 0x0 0x0 0x49 0x45 0x4e 0x44 0xae 0x42 0x60 0x82 "
我尝试使用 clip.encode("hex")
但出现错误
'hex' is not a text encoding
使用 binascii.unhexlify(clip)
,我得到
Odd-length string
如果我删除空格,错误是
Non-hexadecimal digit found
None 关于 SO 或更广泛的互联网的其他讨论正在提供帮助。
您可以使用 python 的内置 eval
函数将字符串中的每个元素计算为整数。
然后在 python2.7 中可以使用内置的 chr
将整数转换为字节:
在 python3 中,您可以使用内置 bytes
转换
>>> clip = clip[:-1] # Remove last space in your clip-string
>>> hex_list = clip.split(' ') # Convert your string to list of hex-characters
# FOR PYTHON3:
>>> byte_string = bytes([eval(h) for h in hex_list])
# FOR PYTHON2.7:
>>> byte_list = [chr(eval(h)) for h in hex_list]
>>> byte_string = ''.join(byte_list) # Convert to string
>>> byte_string
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x08\x00\x00\x00\x07\x08\x02\x00\x00\x00\xba;\x9b\t\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00)IDAT\x08[c\xfc\xff\xff?\x036\xc0\x84M\x10$F+\x89\xf7\x1f\xfe\xfd\xfc\tu\x0b\x8a\x1d9\x95\x9f\xef>\xfc\x0bq\x0e\x00\xfc\x18\rl\xa9\xf7V-\x00\x00\x00\x00IEND\xaeB`\x82'