SRehman
SRehman

Reputation: 469

Conversion of unicode string to hex representation in Python

I have a list of unicode string e.g.

list1 = [u'00 01 02 03']

and I want to convert it into following list

list2 = [0x00, 0x01, 0x02, 0x03]

I have converted list1 to simple string values as follow

list3 = ['0x00', '0x01', '0x02', '0x03'] 

but I need list2 as result.

Any suggestion to produce list2 from list1 directly or from list3.

Thanks

Upvotes: 0

Views: 322

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

If there's always a single string in the original list, a list comprehension will do it quite easily;

list2 = [int(x, 16) for x in list1[0].split()]

...or for multiple strings to int lists, the somewhat more verbose;

list_of_list2s = [[int(x, 16) for x in y.split()] for y in list1]

Upvotes: 2

Related Questions