Re: how to reading binary data...

Discussion in 'Python' started by marcos@nordesia.org, Oct 21, 2004.

  1. Guest

    ## I hope this code could help you.

    def readLong(fin):
    """Reads next long from file"""
    s = fin.read(4)

    if len(s) == 0: # test reaching of end of file
    raise Exception("END-OF-FILE")
    return str2long(s)

    # First element of this list, is the index in a 4 byte string of the
    most-significative-byte, second is the second most-significative and so
    on.
    SIGNIFICANCE = [0,1,2,3] # this list is useful when reading bigendian longs
    #SIGNIFICANCE = [3,2,1,0] # this list is useful when reading littleendian
    longs

    def str2long(s):
    """Returns a long, using the first four byte of a string"""
    l = 0L # the long we will get
    for pos in SIGNIFICANCE: # get each byte in the order specified by the
    significance list
    if pos < len(s):
    c = s[pos]
    else: # if we take a byte that is not in byte string (this happens at
    the end of file when file length is not multiple of 4.
    c = chr(0)

    l*=0x100 # shift left 8 bits
    l+=ord(c) # add the readed byte at the LSB.

    return l

    #Testing
    fi = open("name-of-file","rb")
    run = 1
    while run:
    try:
    print hex(readLong(fi))
    except Exception:
    run=0
    fi.close()


    Regards, marcos.



    >
    > Hello
    >
    > I opened any particular file with the 'rb' mode that is read binary mode.
    > so python will treat the data as a raw data. now i want to read first 4
    > bytes only then i will convert the first 4 bytes into long datatype and
    > then again read 4 bytes and will do the same.
    > But how to set & move the pointer using loop?
    > also how to convert into long?
    >
    > Regards
    > Sandeep
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    , Oct 21, 2004
    #1
    1. Advertising

  2. Peter Hansen Guest

    wrote:
    > SIGNIFICANCE = [0,1,2,3] # this list is useful when reading bigendian longs
    > #SIGNIFICANCE = [3,2,1,0] # this list is useful when reading littleendian
    > longs
    >
    > def str2long(s):
    > """Returns a long, using the first four byte of a string"""
    > l = 0L # the long we will get
    > for pos in SIGNIFICANCE: # get each byte in the order specified by the
    > significance list
    > if pos < len(s):
    > c = s[pos]
    > else: # if we take a byte that is not in byte string (this happens at
    > the end of file when file length is not multiple of 4.
    > c = chr(0)
    >
    > l*=0x100 # shift left 8 bits
    > l+=ord(c) # add the readed byte at the LSB.
    >
    > return l


    Ditch this! It would be much easier, faster,
    more maintainable to use the struct module:

    struct.unpack('<l', four_byte_string)

    This returns an integer from little-endian data, while
    using >l assumes big-endian data.

    -Peter
    Peter Hansen, Oct 21, 2004
    #2
    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. Denise Smith
    Replies:
    2
    Views:
    650
    Denise Smith
    Nov 22, 2003
  2. My Name
    Replies:
    9
    Views:
    10,341
    Roedy Green
    Jul 21, 2004
  3. Brad Marts

    Reading binary data from file

    Brad Marts, Dec 8, 2003, in forum: C++
    Replies:
    1
    Views:
    432
    Victor Bazarov
    Dec 8, 2003
  4. Dimitri Papoutsis

    Problems with reading binary data files

    Dimitri Papoutsis, Mar 10, 2005, in forum: C++
    Replies:
    4
    Views:
    391
    Old Wolf
    Mar 11, 2005
  5. Miki Tebeka

    Re: how to reading binary data...

    Miki Tebeka, Oct 21, 2004, in forum: Python
    Replies:
    1
    Views:
    401
    Bengt Richter
    Oct 21, 2004
Loading...

Share This Page