Jesh Kundem Jesh Kundem - 1 month ago 24
Python Question

Numpy Int Array to HEX and then to String Conversion PYTHON

I have couple of questions

Say I have a numpy array

a = np.array([0,1,2,3,4,31])

a0 = a[0]
a1 = a[1]
a2 = a[2]
a3 = a[3]
a4 = a[4]
a5 = a[5]

print hex(a4), hex(a5)

gives me

0x4L 0x1F

same for a0, a1, a2, a3,a5. I know the L is because of the numpy array.

Now how would I get 0x04 and not 0x4.

My required outcome is


My required answer should start with 0x -- the hex values of a5, a4, a3, a2, a1, a0 - without the OX. The required output is a string. I can do the bit manipulation, if I have the zero. But not without it.


You can try this workaround. An element wise hex conversion and a later join. '0x' is added to the start of the string:

>>> a = np.array([0,1,2,3,4,31])
>>> '0x' + ''.join('{:02X}'.format(i) for i in reversed(a))