Re: Reading a binary file

Discussion in 'Python' started by Steve Holden, Jun 26, 2003.

  1. Steve Holden

    Steve Holden Guest

    "Sorin Marti" <> wrote in message
    news:...
    > 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/
    Steve Holden, Jun 26, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    0
    Views:
    463
  2. El Durango

    reading binary file.

    El Durango, Dec 8, 2004, in forum: Java
    Replies:
    1
    Views:
    7,543
    Gordon Beaton
    Dec 8, 2004
  3. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    903
    James Kanze
    Apr 28, 2008
  4. Guest
    Replies:
    6
    Views:
    1,629
    Guest
    Apr 25, 2010
  5. Jim
    Replies:
    6
    Views:
    719
Loading...

Share This Page