Numpy Int Array to HEX and then to String Conversion PYTHON
<p>I have couple of questions </p>

<p>Say I have a numpy array </p>

<pre><code>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)
</code></pre>

<p>gives me </p>

<pre><code>   0x4L 0x1F
</code></pre>

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

<p>Now how would I get 0x04 and not 0x4. </p>

<p>My required outcome is </p>

<pre><code>'0x1F0403020100'
</code></pre>

<p>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. </p>

<p>What you really want to do is to store your array in a single number by shifting each element of the array by a certain (8) amount of bits:</p>

<pre><code>&gt;&gt;&gt; a = np.array([0,1,2,3,4,31])
&gt;&gt;&gt; hex(sum([ai*256**i for i,ai in enumerate(a)]))
'0x1f0403020100'
</code></pre>

<p>But for this to work, you need to be sure that your array elements are at most 255. That's entirely up to you to keep/check. You should consider using an <code>ndarray</code> of <code>dtype</code> <code>np.uint8</code>, that way there's no chance for you to mangle up the data in your array (since you can't have overflow in your array).</p>

python arrays string numpy