Skip to content Skip to sidebar Skip to footer

How To Convert Hex Str Into Int Array

I have strings of hex, for exemple '01ff6fee32785e366f710df10cc542B4' and I am trying to convert them (efficiently) into an int array 2 characters by 2 characters like [1,255,...].

Solution 1:

You could range(..) over substrings of length 2:

c = '8db6796fee32785e366f710df10cc'c2=[int(c[i:i+2],16) for i in range(0,len(c),2)]

So i iterates of the string with steps of 2 and you take a substring of length 2 from i to i+2 (exclusive) with c[i:i+2]. These you convert by taking int(..,16).

For your sample input it generates:

>>>c='8db6796fee32785e366f710df10cc'>>>[int(c[i:i+2],16) for i inrange(0,len(c),2)]
[141, 182, 121, 111, 238, 50, 120, 94, 54, 111, 113, 13, 241, 12, 12]

The last element is 12 because the length of your string is odd, so it takes c as the last element to parse.

Solution 2:

For an alternate approach

hex_string = '8db6796fee32785e366f710df10cc542B4'
a = bytearray.fromhex(hex_string)
b = list(a)
print(b)

Solution 3:

>>>c = '8db6796fee32785e366f710df10c'>>>[int(x) for x inbytearray.fromhex(c)]
[141, 182, 121, 111, 238, 50, 120, 94, 54, 111, 113, 13, 241, 12]
>>>list(map(int, bytearray.fromhex(c)))
[141, 182, 121, 111, 238, 50, 120, 94, 54, 111, 113, 13, 241, 12]

Two relatively simple solutions, but these will error out if the string has an odd number of characters which might or might not be what you want.

Post a Comment for "How To Convert Hex Str Into Int Array"