NumPy Tutorial
NumPy Useful Resources
Selected Reading
- NumPy - I/O with NumPy
- NumPy - Histogram Using Matplotlib
- NumPy - Matplotlib
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Copies & Views
- NumPy - Byte Swapping
- Sort, Search & Counting Functions
- NumPy - Statistical Functions
- NumPy - Arithmetic Operations
- NumPy - Mathematical Functions
- NumPy - String Functions
- NumPy - Binary Operators
- NumPy - Array Manipulation
- NumPy - Iterating Over Array
- NumPy - Broadcasting
- NumPy - Advanced Indexing
- NumPy - Indexing & Slicing
- Array From Numerical Ranges
- NumPy - Array from Existing Data
- NumPy - Array Creation Routines
- NumPy - Array Attributes
- NumPy - Data Types
- NumPy - Ndarray Object
- NumPy - Environment
- NumPy - Introduction
- NumPy - Home
NumPy Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
NumPy - Byte Swapping
NumPy - Byte Swapping
We have seen that the data stored in the memory of a computer depends on which architecture the CPU uses. It may be pttle-endian (least significant is stored in the smallest address) or big-endian (most significant byte in the smallest address).
numpy.ndarray.byteswap()
The numpy.ndarray.byteswap() function toggles between the two representations: bigendian and pttle-endian.
import numpy as np a = np.array([1, 256, 8755], dtype = np.int16) print Our array is: print a print Representation of data in memory in hexadecimal form: print map(hex,a) # byteswap() function swaps in place by passing True parameter print Applying byteswap() function: print a.byteswap(True) print In hexadecimal form: print map(hex,a) # We can see the bytes being swapped
It will produce the following output −
Our array is: [1 256 8755] Representation of data in memory in hexadecimal form: [ 0x1 , 0x100 , 0x2233 ] Applying byteswap() function: [256 1 13090] In hexadecimal form: [ 0x100 , 0x1 , 0x3322 ]Advertisements