25

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) ;

which displays as

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

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.

1
  • 1
    What version of numpy are you using (np.version.version)? Feb 25, 2012 at 21:25

8 Answers 8

39

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

gives

array([0x1L, 0x2L, 0x3L, 0x4L, 0x5L])

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))})
2
  • 1
    FYI for the OP. The formatter parameter for set_printoptions appears in the 2.0 docs, but not in 1.6. So I imagine it's a new feature. If you have an older version, you could roll your own formatting function and supply it to numpy with numpy.set_string_function. The function you supply would need to format the whole array rather than a single item like in Justin's answer. Feb 25, 2012 at 22:42
  • 3
    For those like myself who doesn't want print options to be changed in a global scope there is a numpy.printoptions helper method now, that you can use like: with np.printoptions(formatter={'int':hex}): Oct 30, 2020 at 21:20
22

Python has a built-in hex function for converting integers to their hex representation (a string). You can use numpy.vectorize to apply it over the elements of the multidimensional array.

>>> import numpy as np
>>> A = np.array([[1,2],[3,4]])
>>> vhex = np.vectorize(hex)
>>> vhex(A)
array([['0x1', '0x2'],
       ['0x3', '0x4']], 
      dtype='<U8')

There might be a built-in method of doing this with numpy which would be a better choice if speed is an issue.

3
  • -0, due to the existence of numpy.set_printoptions. Feb 25, 2012 at 21:24
  • 5
    The vectorize is handy when only an occasional hex string is needed. Otherwise would need to original=get_printoptions(), set_printoptions(hex), print hex, set_printoptions(original) Sep 20, 2012 at 14:14
  • Amazing solution, very pythonic. Jul 20, 2022 at 9:48
6

If what you're looking for it's just for display you can do something like this:

>>> a = [6, 234, 8, 9, 10, 1234, 555, 98]
>>> print '\n'.join([hex(i) for i in a])
0x6
0xea
0x8
0x9
0xa
0x4d2
0x22b
0x62
0
5

Just throwing in my two cents you could do this pretty simply using list comprehension if it's always a 2d array like that

a = [[1,2],[3,4]]
print [map(hex, l) for l in a]

which gives you [['0x1', '0x2'], ['0x3', '0x4']]

3

This one-liner should do the job:

print '[' + '],\n['.join(','.join(hex(n) for n in ar) for ar in array1) + ']'
3
  • 1
    -0, due to the existence of numpy.set_printoptions. Feb 25, 2012 at 21:24
  • Yes, that's definitely a better solution. Question asker, please see Justin Peel's answer, and don't use mine. Feb 25, 2012 at 21:27
  • Numpy is absolutely a better solution! If you have access to numpy. Apr 21, 2016 at 15:21
1

It should be possible to get the behavior you want with numpy.set_printoptions, using the formatter keyword arg. It takes a dictionary with a type specification (i.e. 'int') as key and a callable object returning the string to print. I'd insert code but my old version of numpy doesn't have the functionality yet. (ugh.)

1

I'm using vectorized np.base_repr since I needed my result rjusted with padded 0's

import numpy as np

width = 4
base = 16
array1 = np.array([[25,  160,   154, 233],
                   [61, 244,  198,  248],
                   [227, 226, 141, 72 ],
                   [190, 43,  42, 8]],np.int)


base_v = np.vectorize(np.base_repr)
padded = np.char.rjust(base_v(array1, base), width, '0')
result = np.char.add('0x', padded)

Output:

[['0x0019' '0x00A0' '0x009A' '0x00E9']
 ['0x003D' '0x00F4' '0x00C6' '0x00F8']
 ['0x00E3' '0x00E2' '0x008D' '0x0048']
 ['0x00BE' '0x002B' '0x002A' '0x0008']]
-1
array1_hex = np.array([[hex(int(x)) for x in y] for y in array1])
print array1_hex
# => array([['0x19', '0xa0', '0x9a', '0xe9'],
#           ['0x3d', '0xf4', '0xc6', '0xf8'],
#           ['0xe3', '0xe2', '0x8d', '0x48'],
#           ['0xbe', '0x2b', '0x2a', '0x8']], 
#          dtype='|S4')
1
  • This answer fails with error: TypeError: 'int' object is not iterable
    – tarabyte
    May 15, 2014 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.