Reading a binary file

Steve Holden sholden at holdenweb.com
Thu Jun 26 08:37:39 EDT 2003


"Sorin Marti" <mas at semafor.ch> wrote in message
news:mailman.1056620083.21078.python-list at python.org...
> Hi Andrew,
>
> Thanks for your answer!
>
> Andrew Bennetts wrote:
> > On Thu, Jun 26, 2003 at 10:01:06AM +0200, Sorin Marti wrote:
> >
> >>But now I need the hex values of the binary file.
> >
> > You can get the hex value of a 1-character string with hex(ord(char)),
e.g.:
> >
> >     >>> char = 'a'
> >     >>> hex(ord(char))
> >     '0x61'
> >
>
> That is not exactly what I meant. I've found a solution (a is the binary
> data):
>
> b = binascii.hexlify(a)
>
> For example it gives me C8 which is a HEX-Value. How to change this one
> into a decimal? (The decimal should be 130, right?)
>

If a is the "binary data" (i.e. a one-byte string) then ord(a) is indeed the
answer you want:

>>> import binascii
>>> for c in "ab12+":
...     print binascii.hexlify(c), ord(c)
...
61 97
62 98
31 49
32 50
2b 43
>>>

Or is there something else you aren't telling us?

regards
--
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/







More information about the Python-list mailing list