Search code examples
bytepython-3.8pyserialint64int32

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.


Solution

  • 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'