|
| 1 | +<!-- --- |
| 2 | +title: byteswap() |
| 3 | +description: "Converts the endianness of an array's data items, optionally in place." |
| 4 | +keywords: |
| 5 | + - numpy |
| 6 | + - ndarray |
| 7 | + - byteswap |
| 8 | + - endianness |
| 9 | + - byte order |
| 10 | +type: "function" |
| 11 | +--- |
| 12 | +
|
| 13 | +## Description |
| 14 | +
|
| 15 | +The **`.byteswap()`** method reverses the byte order (or **endianness**) of the array’s data elements. This process essentially swaps the position of bytes within each multi-byte data item (like integers, floats, or complex numbers). |
| 16 | +
|
| 17 | +This feature is primarily used when data must be moved between computer systems that use different byte ordering conventions: |
| 18 | +
|
| 19 | +* **Little-Endian (LE):** Stores the least significant byte first (e.g., Intel/AMD). |
| 20 | +* **Big-Endian (BE):** Stores the most significant byte first (e.g., older network protocols). |
| 21 | +
|
| 22 | +The operation is performed **in place** if the `inplace` parameter is set to `True`, modifying the original array. |
| 23 | +
|
| 24 | +--- |
| 25 | +
|
| 26 | +## Syntax |
| 27 | +
|
| 28 | +```python |
| 29 | +ndarray.byteswap(inplace=False) --> |
| 30 | + |
| 31 | + |
| 32 | +--- |
| 33 | +# REQUIRED METADATA (YAML FRONT MATTER) |
| 34 | +title: numpy.ndarray.byteswap # Specific title |
| 35 | +description: Modifies the byte order (endianness) of the array elements in place or returns a copy with swapped bytes. # Required Description |
| 36 | +keywords: numpy, ndarray, byteswap, endian, endianness, byte order, data representation # Relevant keywords |
| 37 | +type: method # Correct entry type |
| 38 | +--- |
| 39 | + |
| 40 | +The **`.byteswap()`** method reverses the byte order (endianness) of each element in a NumPy array. It's used to ensure data compatibility between systems with different byte storage conventions (little-endian vs. big-endian). |
| 41 | + |
| 42 | +--- |
| 43 | +## Syntax |
| 44 | + |
| 45 | +```python |
| 46 | +array_instance.byteswap(inplace=False) |
| 47 | + |
| 48 | +### Parameters |
| 49 | +Parameter,Type,Description,Default |
| 50 | +inplace,bool,"If True, the byte swap happens directly on the original array (in place), and the method returns the array itself. If False, a new array with swapped bytes is created and returned, leaving the original unchanged.",False |
| 51 | + |
| 52 | +### Return Value |
| 53 | +Returns either the modified original array (if inplace=True) or a new array with the byte order swapped (if inplace=False). The data type (dtype) of the returned array may reflect the new byte order depending on the system and NumPy version, but the underlying byte sequence is always swapped. |
| 54 | + |
| 55 | +## Example |
| 56 | +Consider an array of 16-bit integers. On most systems (like Intel/AMD), these are stored in little-endian format (`<i2`). The number 256 requires two bytes; in little-endian, it's `00 01`. Swapping the bytes makes it `01 00`, which represents a different number if interpreted using the original data type's byte order. |
| 57 | + |
| 58 | +## Codebyte |
| 59 | +import numpy as np |
| 60 | + |
| 61 | +# Create an array of 16-bit integers (usually little-endian by default) |
| 62 | +# 256 in little-endian bytes: 00 01 |
| 63 | +original_array = np.array([1, 256, 512], dtype=np.int16) |
| 64 | +print(f"Original array: {original_array}") |
| 65 | +print(f"Original dtype: {original_array.dtype}") |
| 66 | +print(f"Original bytes (hex): {original_array.tobytes().hex()}") |
| 67 | + |
| 68 | +# Swap bytes, returning a NEW array (inplace=False is default) |
| 69 | +swapped_array = original_array.byteswap() |
| 70 | + |
| 71 | +print("\n--- After byteswap() ---") |
| 72 | +# Values change because the raw bytes are swapped but interpreted via original dtype order |
| 73 | +print(f"Swapped array values (interpreted): {swapped_array}") |
| 74 | +print(f"Swapped array dtype: {swapped_array.dtype}") |
| 75 | +print(f"Swapped bytes (hex): {swapped_array.tobytes().hex()}") # Bytes reversed per element (e.g., 0001 -> 0100) |
| 76 | + |
| 77 | +# Swap bytes IN PLACE |
| 78 | +print("\n--- In-place swap ---") |
| 79 | +# The method returns the modified array when inplace=True |
| 80 | +modified_original = original_array.byteswap(inplace=True) |
| 81 | +print(f"Original array after in-place swap: {original_array}") |
| 82 | +print(f"Return value (inplace=True): {modified_original}") # It's the same array object |
| 83 | +print(f"Original array bytes after swap (hex): {original_array.tobytes().hex()}") |
0 commit comments