The numpy.ndarray.byteswap() function
Swap the bytes of the array elements
Toggle between low-endian and big-endian data representation by returning a byteswapped array, optionally swapped in-place. Arrays of byte-strings are not swapped. The real and imaginary parts of a complex number are swapped individually.
Parameters inplacebool, optional
If True, swap bytes in-place, default is False.
Returns
outndarray
The byteswapped array. If inplace is True, this is a view to self.
import numpy as np
a = np.array([1, 256, 8755], dtype = np.int16)
print ('Our array is:')
print (a)
print ('\n')
print ('Representation of data in memory in hexadecimal form:')
print (map(hex,a))
print ('\n')
# byteswap() function swaps in place by passing True parameter
print ('Applying byteswap() function:')
print (a.byteswap(True))
print ('\n')
print ('In hexadecimal form:')
print (list (map(hex,a)) )
print ('\n')
# We can see the bytes being swapped
If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.
When going through coding examples, it's quite common to have doubts and errors.
If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.
You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.
If you want to