NumPy Byte Swapping
On almost all machines, multi-byte objects are stored as contiguous sequences of bytes. Byte order is the storage rule for program objects spanning multiple bytes.
-
Big-endian mode:It means the high-order byte of data is stored at the low address of memory, while the low-order byte of data is stored at the high address. Such a storage mode is somewhat similar to processing data as a string in order: addresses increase from low to high, while data is placed from high-order to low-order; this is consistent with our reading habit.
-
Little-endian mode:It means the high-order byte of data is stored at the high address of memory, while the low-order byte of data is stored at the low address. This storage mode effectively combines the high and low addresses with the data bit weights: the high-address part has a high weight, and the low-address part has a low weight.
For example, in the C language, a variable x of type int has an address of 0x100, so the value of its corresponding address expression &x is 0x100. And the four bytes of x will be stored at positions 0x100, 0x101, 0x102, 0x103 in memory.

numpy.ndarray.byteswap()
The numpy.ndarray.byteswap() function converts the bytes in each element of the ndarray between big-endian and little-endian.
Example
import
numpy
as
np
a
=
np
.
array
(
[
1
,
256
,
8755
]
,
dtype
=
np
.
int16
)
(
'
Our array is:
'
)
(
a
)
(
'
Represent the data in memory in hexadecimal:
'
)
(
map
(
hex
,
a
)
)
The byteswap() function swaps in place by passing true
(
'
Call the byteswap() function:
'
)
(
a
.
byteswap
(
True
)
)
(
'
Hexadecimal form:
'
)
(
map
(
hex
,
a
)
)
We can see that the bytes have been swapped
The output is:
我们的数组是:
[ 1 256 8755]
Display memory data in hexadecimal: