Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 348
adeel asif Avatar asked Feb 25 '12 21:02

adeel asif


7 Answers

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))})
like image 82
Justin Peel Avatar answered Sep 29 '22 16:09

Justin Peel


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.

like image 20
strcat Avatar answered Sep 26 '22 16:09

strcat


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

like image 27
ameer Avatar answered Sep 25 '22 16:09

ameer


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
like image 45
El Barto Avatar answered Sep 25 '22 16:09

El Barto


This one-liner should do the job:

print '[' + '],\n['.join(','.join(hex(n) for n in ar) for ar in array1) + ']'
like image 43
Gordon Bailey Avatar answered Sep 28 '22 16:09

Gordon Bailey


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

like image 25
senderle Avatar answered Sep 29 '22 16:09

senderle


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']]
like image 34
xyzzyqed Avatar answered Sep 25 '22 16:09

xyzzyqed