# Documentation

### This is machine translation

Translated by
Mouse over text to see original. Click the button below to return to the English verison of the page.

# swapbytes

Swap byte ordering

## Syntax

`Y = swapbytes(X)`

## Description

`Y = swapbytes(X)` reverses the byte ordering of each element in array `X`, converting little-endian values to big-endian (and vice versa). The input array must contain all full, noncomplex, numeric elements.

## Examples

### Example 1

Reverse the byte order for a scalar 32-bit value, changing hexadecimal `12345678` to `78563412`:

```A = uint32(hex2dec('12345678')); B = dec2hex(swapbytes(A)) B = 78563412```

### Example 2

Reverse the byte order for each element of a 1-by-4 matrix:

```X = uint16([0 1 128 65535]) X = 0 1 128 65535 Y = swapbytes(X); Y = 0 256 32768 65535```

Examining the output in hexadecimal notation shows the byte swapping:

```format hex X, Y X = 0000 0001 0080 ffff Y = 0000 0100 8000 ffff```

### Example 3

Create a three-dimensional array `A` of 16-bit integers and then swap the bytes of each element:

```format hex A = uint16(magic(3) * 150); A(:,:,2) = A * 40; A A(:,:,1) = 04b0 0096 0384 01c2 02ee 041a 0258 0546 012c A(:,:,2) = bb80 1770 8ca0 4650 7530 a410 5dc0 d2f0 2ee0 swapbytes(A) ans(:,:,1) = b004 9600 8403 c201 ee02 1a04 5802 4605 2c01 ans(:,:,2) = 80bb 7017 a08c 5046 3075 10a4 c05d f0d2 e02e```

## More About

collapse all

### Tall Array Support

This function fully supports tall arrays. For more information, see Tall Arrays.

## See Also

#### Introduced before R2006a

Was this topic helpful?

Watch now