Changing endian format

Discussion in 'Python' started by Amit Gaur, Jan 24, 2004.

  1. Amit Gaur

    Amit Gaur Guest

    Hi newbie to python here,
    I have a binary file and i need to change the endian format..little to big as well as vice versa..could anyone help me out.
    thanks



    ---------------------------------
    Do you Yahoo!?
    Yahoo! SiteBuilder - Free web site building tool. Try it!
    Amit Gaur, Jan 24, 2004
    #1
    1. Advertising

  2. Amit Gaur <> wrote...

    > Hi newbie to python here,
    > I have a binary file and i need to change the endian format..little to big as well as vice versa..could anyone help me out.


    If you use the struct module to read the file, this appears to be
    handled for you.

    http://python.org/doc/current/lib/module-struct.html


    Example (pack the value 1234 into an unsigned long in little endian,
    then big endian format):

    py> import struct
    py> struct.pack('<L', 1234)
    '\xd2\x04\x00\x00'
    py> struct.pack('>L', 1234)
    '\x00\x00\x04\xd2'


    See also 'struct.unpack', 'socket.htons', and 'socket.htonl'.
    David M. Wilson, Jan 24, 2004
    #2
    1. Advertising

  3. Amit Gaur

    Miki Tebeka Guest

    Hell Amit,

    > I have a binary file and i need to change the endian format..little to big as
    > well as vice versa..could anyone help me out.

    Warning: Not tested!

    #!/usr/bin/env python
    from sys import argv
    from array import array

    a = array("H", open(argv[1], "rb").read())
    a.byteswap()
    open(argv[2], "wb").write(a.tostring())

    HTH.
    Miki
    Miki Tebeka, Jan 25, 2004
    #3
    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. hicham
    Replies:
    2
    Views:
    9,023
    dxcoder
    Jul 2, 2003
  2. Ernst Murnleitner

    float: IEEE, big endian, little endian

    Ernst Murnleitner, Jan 13, 2004, in forum: C++
    Replies:
    0
    Views:
    865
    Ernst Murnleitner
    Jan 13, 2004
  3. invincible

    Little Endian to Big Endian

    invincible, Jun 14, 2005, in forum: C++
    Replies:
    9
    Views:
    14,353
    Old Wolf
    Jun 14, 2005
  4. invincible
    Replies:
    1
    Views:
    546
    red floyd
    Jun 14, 2005
  5. hicham

    convert from big-endian to little-endian

    hicham, Jun 30, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,523
    hicham
    Jun 30, 2003
Loading...

Share This Page