Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy tobytes() with defined byteorder

Is it possible to define byte order when converting a numpy array to binary string (with tobytes())?

I would want to force little endianness, but I don't want byte-swapping if it is not necessary.

like image 923
Medical physicist Avatar asked Apr 11 '19 06:04

Medical physicist


1 Answers

When interfacing with C code I use this pattern

numpy.ascontiguousarray(x, dtype='>i4')

That dtype string specifies the endianess and precise bit width.

You can check ndarray.flags to see if conversions are necessary.

like image 180
Mihai Andrei Avatar answered Sep 21 '22 23:09

Mihai Andrei