Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Numpy - Converting a numpy array of hex strings to integers

Tags:

python

numpy

I have a numpy array of hex string (eg: ['9', 'A', 'B']) and want to convert them all to integers between 0 255. The only way I know how to do this is use a for loop and append a seperate numpy array.

import numpy as np

hexArray = np.array(['9', 'A', 'B'])

intArray = np.array([])
for value in hexArray:
    intArray = np.append(intArray, [int(value, 16)])

print(intArray) # output: [ 9. 10. 11.]

Is there a better way to do this?

like image 494
Dave1551 Avatar asked Jan 26 '23 05:01

Dave1551


2 Answers

A vectorized way with array's-view functionality -

In [65]: v = hexArray.view(np.uint8)[::4]

In [66]: np.where(v>64,v-55,v-48)
Out[66]: array([ 9, 10, 11], dtype=uint8)

Timings

Setup with given sample scaled-up by 1000x -

In [75]: hexArray = np.array(['9', 'A', 'B'])

In [76]: hexArray = np.tile(hexArray,1000)

# @tianlinhe's soln
In [77]: %timeit [int(value, 16) for value in hexArray]
1.08 ms ± 5.67 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

# @FBruzzesi soln
In [78]: %timeit list(map(functools.partial(int, base=16), hexArray))
1.5 ms ± 40.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

# From this post
In [79]: %%timeit
    ...: v = hexArray.view(np.uint8)[::4]
    ...: np.where(v>64,v-55,v-48)
15.9 µs ± 294 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
like image 69
Divakar Avatar answered Jan 27 '23 19:01

Divakar


With the use of list comprehension:

 array1=[int(value, 16) for value in hexArray]
 print (array1)

output:

[9, 10, 11]
like image 43
tianlinhe Avatar answered Jan 27 '23 19:01

tianlinhe