打印整数数组为十六进制数字 [英] Print an integer array as hexadecimal numbers

查看:221
本文介绍了打印整数数组为十六进制数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以创建一个数组

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

注:十六进制数字可能不是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位平台上,它是发送渴望格式化。为了解决这个问题,你可以使用

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆