Reading hex to int from a binary string
Jack Norton
jack at 0x6a.com
Fri Oct 9 09:32:56 EDT 2009
Luc wrote:
> Hi all,
>
> I read data from a binary stream, so I get hex values as characters
> (in a string) with escaped x, like "\x05\x88", instead of 0x05.
>
> I am looking for a clean way to add these two values and turn them
> into an integer, knowing that calling int() with base 16 throws an
> invalid literal exception.
>
> Any help appreciated, thanks.
>
Hi,
Check out the ord() function.
Example:
x = '\x34'
print ord(x)
output: 52
Also, if you, lets say read(4), and end up with `x = '\x05\x41\x24\x00'
you can use x[i] to address each character. So if you
print x[1]
output: 'A'
That should be enough to get you started in the right direction.
-Jack
More information about the Python-list
mailing list