numpy.ndarray.byteswap () in Python

numpy.ndarray.byteswap() toggles between low endian and big endian data representation by returning a byte-swapped array that can be 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:

# 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 ))

Output:

 [256 1 25600] 

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

# 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 ))

Output:

 ValueError Traceback (most recent call last) in () 1 import numpy as geek ---- & gt; 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`