To make this work with negative integers you need to mask your input with your preferred int type output length. For example, -16 as a little endian uint32_t would be bytearray.fromhex(hex(-16 & (2**32-1))[2:])[::-1], which evaluates to bytearray(b'\xf0\xff\xff\xff')
我知道这是一条老线索,但它仍然有用。这是我用Python的两分钱:
所以,关键是将其转换为bytearray并将其反转。 一行:
注:您可以将“bytearray”数据视为“字节”,甚至可以将它们与
b'raw bytes'
混合使用更新: 作为coments中的Will点,您还可以管理负整数:
您需要使用^{} module :
这里
<
表示小结束符,并且Q
表示我们要打包一个无符号的长(8字节)。请注意,Python将对可打印ASCII范围内的任何字节使用ASCII字符来表示生成的bytestring,因此上述结果的
14OJ
、24O
和J
部分:相关问题 更多 >
编程相关推荐