将整数数组打印为十六进制数 [英] Print an integer array as hexadecimal numbers
本文介绍了将整数数组打印为十六进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个使用创建的数组
I have an array created by using
array1 = np.array([[25, 160, 154, 233],
[61, 244, 198, 248],
[227, 226, 141, 72 ],
[190, 43, 42, 8]],np.int) ;
显示为
[[25, 160, 154, 233]
[61, 244, 198, 248]
[227, 226, 141, 72]
[190, 43, 42 , 8]]
如何将此数组显示为十六进制数字,如下所示:
How do I display this array as hexadecimal numbers like this:
[[0x04, 0xe0, 0x48, 0x28]
[0x66, 0xcb, 0xf8, 0x06]
[0x81, 0x19, 0xd3, 0x26]
[0xe5, 0x9a, 0x7a, 0x4c]]
注意:hex 中的数字可能不是 int 中数字的真正转换.我已经填充了十六进制数组只是为了举例说明我需要什么.
Note: numbers in hex may not be real conversions of numbers in int. I have filled hex array just to give example of what I need.
推荐答案
您可以设置 numpy 的打印选项来执行此操作.
You can set the print options for numpy to do this.
import numpy as np
np.set_printoptions(formatter={'int':hex})
np.array([1,2,3,4,5])
给予
array([0x1L, 0x2L, 0x3L, 0x4L, 0x5L])
最后的 L 只是因为我在 64 位平台上,它正在向格式化程序发送 long.要解决此问题,您可以使用
The L at the end is just because I am on a 64-bit platform and it is sending longs to the formatter. To fix this you can use
np.set_printoptions(formatter={'int':lambda x:hex(int(x))})
这篇关于将整数数组打印为十六进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文