numpy array - convert hex to int

Piet van Oostrum piet-l at vanoostrum.org
Tue Sep 10 07:40:42 EDT 2019


Sharan Basappa <sharan.basappa at gmail.com> writes:

> On Sunday, 8 September 2019 11:16:52 UTC-4, Luciano Ramalho  wrote:
>> >>> int('C0FFEE', 16)
>> 12648430
>> 
>> There you go!
>> 
>> On Sun, Sep 8, 2019 at 12:02 PM Sharan Basappa <sharan.basappa at gmail.com> wrote:
>> >
>> > I have a numpy array that has data in the form of hex.
>> > I would like to convert that into decimal/integer.
>> > Need suggestions please.
>> > --
>
> I am sorry. I forgot to mention that I have the data in a numpy array.
> So, when I try to convert to int, I get the following error.
>
> sample code here
> #####################
> my_data_3 = int(my_data_2)
>
> my_data_4 = my_data_3.astype(np.float)
> #####################
>
> Error here
> ###############
> #np.core.defchararray.replace(my_data_2,",'')
>      27 
> ---> 28 my_data_3 = int(my_data_2)
>      29 
>      30 my_data_4 = my_data_3.astype(np.float)
> TypeError: only length-1 arrays can be converted to Python scalars 
> #########################

>>> my_data_2 = numpy.array(['0a', '2f', '38', 'ff'])
>>> 
>>> a_toint = np.frompyfunc(lambda x: int(x, 16), 1, 1)
>>> my_data_3 = a_toint(my_data_2)
>>> my_data_3
array([10, 47, 56, 255], dtype=object)

-- 
Piet van Oostrum <piet-l at vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]



More information about the Python-list mailing list