swapbytes
Reverses the byte order of integers (endianness swap).
Category: Language Fundamentals > Operators and Elementary Operations
Syntax
Y = swapbytes(X)
Reverses the bytes of each element of X.
Description
Data from files or network protocols may be stored with the opposite byte order from the one your machine uses. swapbytes converts between little-endian and big-endian: swapbytes(uint16(1)) is 256, because the bytes 00 01 become 01 00, and swapbytes(uint32(1)) is 16777216.
Applying it twice gives back the original value. 8-bit types have one byte, so they are unchanged.
Input arguments
X- Array of an integer class (
int16,uint16,int32,uint32,int64,uint64) orsingleordouble.
Output arguments
Y- Array of the same class and size as
Xwith swapped byte order.
Example
swapbytes(uint16(1))
swapbytes(uint16([1 256]))
swapbytes(uint32(1))
Output
ans = uint16
256
ans = 1×2 uint16 row vector
256 1
ans = uint32
16777216