Updated On : Jul-22,2020 Tags numpy, basics
Numpy Tutorial - Byte Swapping Part 18

Learning Numpy - Simple Tutorial For Beginners - NumPy Byte Swapping Part 18

numpy.ndarray.byteswap()

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.
In [8]:
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
Our array is:
[   1  256 8755]


Representation of data in memory in hexadecimal form:
<map object at 0x7fd33c37a358>


Applying byteswap() function:
[  256     1 13090]


In hexadecimal form:
['0x100', '0x1', '0x3322']



  Support Us to Make a Difference

Thank You for visiting our website. If you like our work, please support us so that we can keep on creating new tutorials/blogs on interesting topics (like AI, ML, Data Science, Python, Digital Marketing, SEO, etc.) that can help people learn new things faster. You can support us by clicking on the Coffee button at the bottom right corner. We would appreciate even if you can give a thumbs-up to our article in the comments section below.

 Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to let us know in the comments section below (Guest Comments are allowed). We appreciate and value your feedbacks.



Dolly Solanki  Dolly Solanki