Numpy-byte-swapping

提供:Dev Guides
移動先:案内検索

NumPy-バイト交換

コンピュータのメモリに保存されるデータは、CPUが使用するアーキテクチャに依存することがわかりました。 リトルエンディアン(最下位アドレスが最小アドレスに格納される)またはビッグエンディアン(最小アドレスの最上位バイト)になります。

numpy.ndarray.byteswap()

  • numpy.ndarray.byteswap()*関数は、ビッグエンディアンとリトルエンディアンの2つの表現を切り替えます。
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

それは次の出力を生成します-

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']