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.
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.
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.
© 2022 - 2024 — McMap. All rights reserved.
astype
. Set the desired endedness in the dtype. – Merimeridabytes
always involve a copying. In order not to involve another copying when byte-swapping, usea[:,None].view('B')[:,::-1].tobytes()
to output opposite-endian (opposite to what's described in thedtype
, which is by default little) – Huerta