返回介绍

字节交换(Byte Swapping)

发布于 2021-06-12 09:43:38 字数 876 浏览 1464 评论 0 收藏 0

我们已经看到存储在计算机内存中的数据取决于CPU使用的体系结构。 它可能是little-endian(最低有效存储在最小地址中)或big-endian(最小地址中最高有效字节)。

numpy.ndarray.byteswap()

numpy.ndarray.byteswap()函数在两个表示之间切换:bigendian和little-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

它将产生以下输出 -

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文