Reading hex to int from a binary string

L

Luc

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.
 
D

Diez B. Roggisch

Luc said:
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.

Consider this (in the python interpreter):
255

In other words: no, you *don't* get hex values. You get bytes from the
stream "as is", with python resorting to printing these out (in the
interpreter!!!) as "\xXX". Python does that so that binary data will
always have a "pretty" output when being inspected on the REPL.

But they are bytes, and to convert them to an integer, you call "ord" on
them.

So assuming your string is read bytewise into two variables a & b, this
is your desired code:
415


HTH, Diez
 
L

Luc

Consider this (in the python interpreter):

 >>> chr(255)
'\xff'
 >>> chr(255) == r"\xff"
False
 >>> int(r"ff", 16)
255

In other words: no, you *don't* get hex values. You get bytes from the
stream "as is", with python resorting to printing these out (in the
interpreter!!!) as "\xXX". Python does that so that binary data will
always have a "pretty" output when being inspected on the REPL.

But they are bytes, and to convert them to an integer, you call "ord" on
them.

So assuming your string is read bytewise into two variables a & b, this
is your desired code:

 >>> a = "\xff"
 >>> b = "\xa0"
 >>> ord(a) + ord(b)
415

HTH, Diez

Sorry I was not clear enough. When I said "add", I meant concatenate
because I want to read 0x0588 as one value and ord() does not allow
that.

However you pointed me in the right direction and I found that int
(binascii.hexlify(a + b, 16)) does the job.

Thanks.
 
D

Dennis Lee Bieber

Sorry I was not clear enough. When I said "add", I meant concatenate
because I want to read 0x0588 as one value and ord() does not allow
that.

However you pointed me in the right direction and I found that int
(binascii.hexlify(a + b, 16)) does the job.
Yeesh... This is what struct is designed for...
ˆand more
8805, a0d

You may need to adjust for expected endian mode...
 
L

Luc

        Yeesh... This is what   struct  is designed for...


 ˆand more




8805,  a0d

        You may need to adjust for expected endian mode...

3338

Nice, thanks!
 
D

Diez B. Roggisch

Luc said:
Sorry I was not clear enough. When I said "add", I meant concatenate
because I want to read 0x0588 as one value and ord() does not allow
that.

(ord(a) << 8) + ord(b)


Diez
 
J

Jack Norton

Luc said:
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 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
 
L

Luc

Luc schrieb:





(ord(a) << 8) + ord(b)

Diez

Yes that too. But I have four bytes fields and single bit fields to
deal with as well so I'll stick with struct.

Thanks.
 
D

Diez B. Roggisch

Luc said:
Yes that too. But I have four bytes fields and single bit fields to
deal with as well so I'll stick with struct.

For the future: it helps describing the actual problem, not something
vaguely similar - this will get you better answers, and spare those who
try to help you the effort to come up with solutions that aren't ones.

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top