Search code examples
pythonlistbinaryint32

convert list of bytes to int32 in python3


i have a list of several 4x6 bytes (32bit) values in byte format. Is there any possibility to convert these binary values efficient in 32bit integers in python?

There is the method int.from_bytes(xTest, byteorder='little', signed = True), but it would be very inefficient to loop through all the values. Maybe someone have an idea?

xTest = [b'\xf8\x80[\xf0', b'\x12\x81\x87\xef', b'-\x81\xc0\xee', b'I\x81\xf9\xed']


Solution

  • Use list comprehension to apply the same operator to each of the element of your original xTest list:

    int_list = [int.from_bytes(x, byteorder='little', signed = True) for x in xTest]
    

    Here x loops through each of the element (thus, your 4 bytes) of xTest and thus allows you to apply the same operator to each of them.