0

我有一个考虑 python3 的问题。我已经尝试了几天来创建一个字节对象,如下所示:

b"'\x10\x00\x0020180425"

第一部分 '\x10\x00\x00 是两个组合在一起的 int16 对象。这就是我尝试将它们组合起来的方式:

array = [0,0,0,0,1,9,7,0,0,1,0,1]
blebytes = bytearray(array)

z = np.int16(20)
blebytes[0] = (z & 0xFF00) >> 8
blebytes[1] = (z & 0x00FF)

z = np.int16(123)
blebytes[2] = (z & 0xFF00) >> 8
blebytes[3] = (z & 0x00FF)

第二部分(20180425)就是今天的日期。我正在尝试这样做:

datestr = time.strftime("%Y%m%d")
for i in range(0,8):
    blebytes[i+4] = np.int16(datestr[i])
print(blebytes)

但是如果我打印我的 blebytes 数组,它看起来像这样:

bytearray(b'\x04\xe8\t\xf7\x02\x00\x01\x08\x00\x04\x02\x05')

我究竟做错了什么?有人能帮我吗?

4

0 回答 0