numpy.ndarray.byteswap() in Python

numpy.ndarray.byteswap() function toggle between low-endian and big-endian data representation by returning a byteswapped array, optionally swapped in-place.

Syntax: ndarray.byteswap(inplace=False)

Parameters:
inplace : [bool, optional] If True, swap bytes in-place, default is False.

Returns:
out : [ndarray] The byteswapped array. If inplace is True, this is a view to self.

Code #1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining 
# byteswap() function 
import numpy as geek
  
# a is an array of integers.
a = geek.array([1, 256, 100], dtype = np.int16)
   
print(a.byteswap(True))

chevron_right


Output :

[256  1  25600]

 

Code #2: byteswap() function does not work on arrays of strings.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining 
# byteswap() function 
import numpy as geek
  
# a is an array of strings
a = geek.array(["arka","soumen","simran"],dtype = np.int16)
  
print(a.byteswap(True))

chevron_right


Output :

ValueError                                Traceback (most recent call last)
 in ()
      1 import numpy as geek
----> 2 a = geek.array(["arka","soumen","simran"],dtype = np.int16)
      3 
      4 #a is an array of strings
      5 

ValueError: invalid literal for int() with base 10: 'arka'


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.