Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: read a bigendian file python

  1. #1
    Join Date
    Jul 2005
    Location
    London,England
    Beans
    1,022
    Distro
    Ubuntu Karmic Koala (testing)

    read a bigendian file python

    hi, im trying to read a 4 byte integer from a binary file, only trouble is, the file is in bigendian format and no matter what i try i cant seem to make python read it big endian,

    does anyone know how to read big endian files in python
    Jeremy Clarkson - "A Dazzling Hero of Political Incorrectness"
    My LastFM profile
    want to Give each GTK program its own theme? well look Here

  2. #2
    Join Date
    Dec 2006
    Beans
    57

    Re: read a bigendian file python

    is there any chance you can send me this big-endian file ? or a sample big-endian file ?

  3. #3
    Join Date
    Dec 2006
    Beans
    57

    Re: read a bigendian file python

    well, if it is just an 4 bytes integer you could do this:

    from struct import unpack
    file = open('file', 'rb')

    be_int = file.read(4)

    le_int = unpack('>L', be_int) # > for big endian, < for little endian

  4. #4
    Join Date
    Jul 2005
    Location
    London,England
    Beans
    1,022
    Distro
    Ubuntu Karmic Koala (testing)

    Re: read a bigendian file python

    im willing to bet you have a bigendian file on your computer any mp3 is big endian, (or at least the metadata is)
    Jeremy Clarkson - "A Dazzling Hero of Political Incorrectness"
    My LastFM profile
    want to Give each GTK program its own theme? well look Here

  5. #5
    Join Date
    Dec 2006
    Beans
    57

    Re: read a bigendian file python

    it gets complicated to read variable lenght data in big endian format while im using a little endian machine, but if you know the lenght of data, like int 4 bytes it is a lot easier. Did you try the solution I told you ?

  6. #6
    Join Date
    Jul 2005
    Location
    London,England
    Beans
    1,022
    Distro
    Ubuntu Karmic Koala (testing)

    Re: read a bigendian file python

    i get a
    global name 'unpack' is not defined
    error is there something i need to import?
    Jeremy Clarkson - "A Dazzling Hero of Political Incorrectness"
    My LastFM profile
    want to Give each GTK program its own theme? well look Here

  7. #7
    Join Date
    Dec 2006
    Beans
    57

    Re: read a bigendian file python

    first line: from struct import unpack

  8. #8
    Join Date
    Jul 2005
    Location
    London,England
    Beans
    1,022
    Distro
    Ubuntu Karmic Koala (testing)

    Re: read a bigendian file python

    ah yeah sorry, my bad missed it

    still get an erro though,

    File "basictreeview (copy).py", line 312, in add_event
    self.import_meta_info(lotsafiles.pop())
    File "basictreeview (copy).py", line 268, in import_meta_info
    title = self.import_id3v2_title(data)
    File "basictreeview (copy).py", line 216, in import_id3v2_title
    le_int = unpack('>',last)
    struct.error: unpack str size does not match format
    thx 4 ur help btw


    the code is this

    f = open(data, "rb")
    from struct import unpack
    f.seek(6)
    x = 0
    last = f.read(4)
    le_int = unpack('>',last)
    print last
    Jeremy Clarkson - "A Dazzling Hero of Political Incorrectness"
    My LastFM profile
    want to Give each GTK program its own theme? well look Here

  9. #9
    Join Date
    Dec 2006
    Beans
    57

    Re: read a bigendian file python

    that is why I asked you to send me the file, I just can't guess at what position your integer would be.

  10. #10
    Join Date
    Jul 2005
    Location
    London,England
    Beans
    1,022
    Distro
    Ubuntu Karmic Koala (testing)

    Re: read a bigendian file python

    it is an mp3 file, its always at the same place because of the specification, cant really send an mp3 file

    read from the ver start of the file

    first 3 bytes always = "ID3"
    next 2 bytes will be "0n 00" where n is a number from 1 to 4
    next byte needs to be read as binary for flags
    next 4 bytes are the size of the metadata, in this case its "00 00 08 67" which when read big endian is 2151 (right) in little endian is 1728577536 (not right)
    ive verified in a hex editor too
    Jeremy Clarkson - "A Dazzling Hero of Political Incorrectness"
    My LastFM profile
    want to Give each GTK program its own theme? well look Here

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •