不太复杂,或者我希望如此。我有一个 256 位十六进制整数编码为大端,我需要将其转换为小端。Python 的 struct 模块通常就足够了,但官方文档没有列出大小甚至接近我需要的格式。
使用 struct 的非长度特定类型(尽管我可能做错了)似乎不起作用:
>> x = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000'
>> y = struct.unpack('>64s', x)[0] # Unpacking as big-endian
>> z = struct.pack('<64s', y) # Repacking as little-endian
>> print z
'ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000'
示例代码(应该发生什么):
>> x = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000'
>> y = endianSwap(x)
>> print y
'00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff'