user20035360
user20035360

Reputation: 21

Python3.8 pySerial sending int64 as signed bytes

I'm trying to convert a class 'numpy.int64'named "int_array" into bytes. In the past I used this structure

(-1024).to_bytes(8, byteorder='big', signed=True)

and worked fine. Now, saving all the integers into a matrix, it doesn't allow me to do this:

int_array[1][i].to_bytes(8, byteorder='big', signed=True)

If there is a function for integer32 it would also work.

Does anybody know an equivalent command? I will appreciate any help.

Upvotes: 0

Views: 261

Answers (1)

HTF
HTF

Reputation: 7300

Apparently, in Python 3.8 I'm not allowed to do that.

The int.to_bytes method is available since Python 3.2:

New in version 3.2.

Alternatively you can use the struct module:

>>> import struct
>>> struct.pack(">q", -1024)
b'\xff\xff\xff\xff\xff\xff\xfc\x00'

Upvotes: 1

Related Questions