String of bytes in int

I want to convert a string like this to int: s = 'A0 00 00 00 63' . What is the easiest / best way to do this?

For example, '20 01' should become 8193 (2 * 16 ^ 3 + 1 * 16 ^ 0 = 8193).

+4
source share
2 answers

Use int() with str.split() :

 In [31]: s='20 01' In [32]: int("".join(s.split()),16) Out[32]: 8193 

or str.replace() and pass the base as 16:

 In [34]: int(s.replace(" ",""),16) Out[34]: 8193 

Here both split() and replace() convert '20 01' to '2001' :

 In [35]: '20 01'.replace(" ","") Out[35]: '2001' In [36]: "".join('20 01'.split()) Out[36]: '2001' 
+12
source
 >>> s = 'A0 00 00 00 63' >>> s = s.replace(" ","") >>> print s A000000063 >>> for i in xrange(0,len(s),4): print int(s[i:i+3],16) 2560 0 99 
0
source

All Articles