Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Reading hex to int from a binary string

Reply
Thread Tools

Reading hex to int from a binary string

 
 
Luc
Guest
Posts: n/a
 
      10-08-2009
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.
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      10-08-2009
Luc schrieb:
> 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):

>>> 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
 
Reply With Quote
 
 
 
 
Luc
Guest
Posts: n/a
 
      10-08-2009
On Oct 8, 11:13*pm, "Diez B. Roggisch" <(E-Mail Removed)> wrote:
> Luc schrieb:
>
> > 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):
>
> *>>> 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.

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      10-09-2009
On Thu, 8 Oct 2009 14:52:33 -0700 (PDT), Luc <(E-Mail Removed)>
declaimed the following in gmane.comp.python.general:

> On Oct 8, 11:13*pm, "Diez B. Roggisch" <(E-Mail Removed)> wrote:
> > Luc schrieb:
> >
> > > 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):
> >
> > *>>> 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.
>

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

>>> import struct
>>> something = "\x05\x88and more\r\n"
>>> print something

łand more

>>>
>>> (h1, st, h2) = struct.unpack("H8sh", something)
>>> h1

34821
>>> st

'and more'
>>> h2

2573
>>>
>>> print "%4x, %4x" % (h1, h2)

8805, a0d

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

>>> (h1, st, h2) = struct.unpack(">H8sh", something)
>>> print "%4.4x, %4.4x" % (h1, h2)

0588, 0d0a
>>> h1

1416
>>> h2

3338
>>>

--
Wulfraed Dennis Lee Bieber KD6MOG
http://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
Luc
Guest
Posts: n/a
 
      10-09-2009
On Oct 9, 3:12┬*am, Dennis Lee Bieber <(E-Mail Removed)> wrote:
> On Thu, 8 Oct 2009 14:52:33 -0700 (PDT), Luc <(E-Mail Removed)>
> declaimed the following in gmane.comp.python.general:
>
>
>
> > On Oct 8, 11:13┬*pm, "Diez B. Roggisch" <(E-Mail Removed)> wrote:
> > > Luc schrieb:

>
> > > > 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):

>
> > > ┬*>>> 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.

>
> ┬* ┬* ┬* ┬* Yeesh... This is what ┬* struct ┬*is designed for...
>
> >>> import struct
> >>> something = "\x05\x88and more\r\n"
> >>> print something

>
> ┬*╦ćand more
>
>
>
> >>> (h1, st, h2) = struct.unpack("H8sh", something)
> >>> h1

> 34821
> >>> st

> 'and more'
> >>> h2

> 2573
>
> >>> print "%4x, %4x" % (h1, h2)

>
> 8805, ┬*a0d
>
> ┬* ┬* ┬* ┬* You may need to adjust for expected endian mode...
>
> >>> (h1, st, h2) = struct.unpack(">H8sh", something)
> >>> print "%4.4x, %4.4x" % (h1, h2)

> 0588, 0d0a
> >>> h1

> 1416
> >>> h2

> 3338
>
> --
> ┬* ┬* ┬* ┬* Wulfraed ┬* ┬* ┬* ┬* Dennis Lee Bieber ┬* ┬* ┬* ┬* ┬* ┬* ┬* KD6MOG
> ┬* ┬* ┬* ┬* (E-Mail Removed) ┬* ┬* HTTP://wlfraed.home.netcom.com/


Nice, thanks!
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      10-09-2009
Luc schrieb:
> On Oct 8, 11:13 pm, "Diez B. Roggisch" <(E-Mail Removed)> wrote:
>> Luc schrieb:
>>
>>> 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):
>>
>> >>> 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.


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


Diez
 
Reply With Quote
 
Jack Norton
Guest
Posts: n/a
 
      10-09-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
 
Reply With Quote
 
Luc
Guest
Posts: n/a
 
      10-09-2009
On Oct 9, 10:45*am, "Diez B. Roggisch" <(E-Mail Removed)> wrote:
> Luc schrieb:
>
>
>
> > On Oct 8, 11:13 pm, "Diez B. Roggisch" <(E-Mail Removed)> wrote:
> >> Luc schrieb:

>
> >>> 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):

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

>
> (ord(a) << + 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.

 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      10-10-2009
Luc schrieb:
> On Oct 9, 10:45 am, "Diez B. Roggisch" <(E-Mail Removed)> wrote:
>> Luc schrieb:
>>
>>
>>
>>> On Oct 8, 11:13 pm, "Diez B. Roggisch" <(E-Mail Removed)> wrote:
>>>> Luc schrieb:
>>>>> 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):
>>>> >>> 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.

>> (ord(a) << + 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.


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex value in string back to real hex value jack Python 4 09-08-2004 07:11 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments