在Ruby中,我可以轻松地将表示某些序列的数组包装到二进制字符串中:
# for int
# "S*!" directive means format for 16-bit int, and using native endianess
# 16-bit int, so each digit was represented by two bytes. "\x01\x00" and "\x02\x00"
# here the native endianess is "little endian", so you should
# look at it backwards, "\x01\x00" becomes 0001, and "\x02\x00" becomes 0002
"\x01\x00\x02\x00".unpack("S!*")
# [1, 2]
# for hex
# "H*" means every element in the array is a digit for the hexstream
["037fea0651b358c361de"].pack("H*")
# "\x03\x7F\xEA\x06Q\xB3X\xC3a\xDE"
我找不到在python中将序列转换为字节(反之亦然)的统一且等效的方法。
尽管struct
提供了用于打包为字节对象的方法,但format string却没有十六进制流的选项。
编辑:我真正想要的是功能与Ruby的arr.pack和str.unpack一样多的东西,它支持多种格式和字节序控制。
答案 0 :(得分:2)
struct
仅执行与诸如C结构之类的内存转储相对应的固定宽度编码。您需要bytes.fromhex
或binascii.unhexlify
,具体取决于源类型(从不列出)。
进行任何此类转换后,您可以对包含与格式字符串相对应的任意数量个“记录”的字节字符串使用struct.unpack
;每个都解码为返回的元组的元素。格式字符串支持通常的整数大小和字节序选择;当然可以动态构造一种格式来执行类似读取运行时选择尺寸的矩阵的操作:
mat=struct.unpack("%dd"%cols,buf) # rows determined from len(buf)
如果元素类型为原始,则还可以构造低级内存 array
;那么您可以将byteswap
设为Alec A mentioned。 NumPy提供similar facilities。
答案 1 :(得分:2)
对于utf-8范围内的字符串,应为:
from binascii import unhexlify
strg = "464F4F"
unhexlify(strg).decode() # FOO (str)
如果您的内容只是二进制文件
strg = "037fea0651b358c361de"
unhexlify(strg) # b'\x03\x7f\xea\x06Q\xb3X\xc3a\xde' (bytes)
bytes.fromhex
(如Davis Herring's answer)也值得一试。
答案 2 :(得分:1)
尝试memoryview.cast
,它允许您更改数组或字节对象的字节序。
将值存储为arrays使事情变得更容易,因为您可以使用 PM> Install-Package Newtonsoft.Json -Version 9.0.1
函数。