5

I am looking for a way to convert an array of strings in numpy to the integers they represent in hexadecimal. So in other words, the array version of:

int("f040", 16)

I can convert a string array to integers base-10 by calling arr.astype(numpy.int32), but I can't see any obvious way to convert them base-16. Does anyone know of a way to do this?

1 Answer 1

3
ar = ['f040', 'deadbeaf'] 
int_array = [int(a, 16) for a in ar]
print int_array

output:

[61504, 3735928495L]

Sign up to request clarification or add additional context in comments.

3 Comments

You don't need to prefix a with '0x'
I am aware that list comprehensions can solve the problem, I was wondering if there was a "fast" method in numpy itself analogous to astype.
@dpitch40 Probably there is not a fast answer, although the list comprehension is only a very short line. Or do you mean performance wise? (numpy is generally about 10 times faster than Python functions).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.