Bài 18: Chuyển đổi Byte - Numpy trong Python

Đăng bởi: Admin | Lượt xem: 1253 | Chuyên mục: AI


Chúng ta đã thấy rằng dữ liệu được lưu trữ trong bộ nhớ của máy tính phụ thuộc vào kiến trúc mà CPU sử dụng. Nó có thể là little-endian (ít quan trọng nhất được lưu trữ trong địa chỉ nhỏ nhất) hoặc big-endian (byte quan trọng nhất trong địa chỉ nhỏ nhất).

numpy.ndarray.byteswap()

Hàm numpy.ndarray.byteswap () chuyển đổi giữa hai biểu diễn: bigendian và little-endian.
Ví dụ :
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
Kết quả :
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']
Bài tiếp theo: Copy & View >>
vncoder logo

Theo dõi VnCoder trên Facebook, để cập nhật những bài viết, tin tức và khoá học mới nhất!