Endianness conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Toby

    #1

    Endianness conversion

    As part of a program I'm writing, I need to save to disk big amounts of
    data (hundreds of MB, in 8kB chunks) swapping every couple of bytes.

    I obviously cannot do it in a Python loop.

    Is there a function I could use in the standard library, or do I have to
    write my own C extension?


    Toby
  • Dan Sommers

    #2
    Re: Endianness conversion

    On Sat, 24 Feb 2007 15:39:53 +0000, Toby wrote:
    As part of a program I'm writing, I need to save to disk big amounts of
    data (hundreds of MB, in 8kB chunks) swapping every couple of bytes.
    >
    I obviously cannot do it in a Python loop.
    >
    Is there a function I could use in the standard library, or do I have to
    write my own C extension?
    You could try the struct module. If your input comes in fixed sized
    chunks, just call struct.unpack and struct.pack once per chunk.

    HTH,
    Dan

    --
    Dan Sommers
    <http://www.tombstoneze ro.net/dan/>
    Atoms are not things. -- Werner Heisenberg.

    Comment

    • Toby

      #3
      Re: Endianness conversion

      Dan Sommers wrote:
      You could try the struct module. If your input comes in fixed sized
      chunks, just call struct.unpack and struct.pack once per chunk.
      Thanks, but it was a bit awkward to use for big chunks.

      I ended up writing my own byteswapper in Pyrex:


      def swapbytes(data) :
      "Swap every two bytes of a even-sized python string, in place"
      cdef int i
      cdef char t, *p
      p = data
      for i from 0 <= i < len(data) / 2:
      t = p[0]
      p[0] = p[1]
      p[1] = t
      p = p + 2


      Toby

      Comment

      • Daniel Harding

        #4
        Re: Endianness conversion

        On Feb 24, 9:39 am, Toby <etat...@gmail. comwrote:
        As part of a program I'm writing, I need to save to disk big amounts of
        data (hundreds of MB, in 8kB chunks) swapping every couple of bytes.
        >
        I obviously cannot do it in a Python loop.
        >
        Is there a function I could use in the standard library, or do I have to
        write my own C extension?
        Try the using the array module. array objects provide a byteswap
        method which reverses endianness.

        -Daniel

        Comment

        • Toby

          #5
          Re: Endianness conversion

          Daniel Harding wrote:
          Try the using the array module. array objects provide a byteswap
          method which reverses endianness.
          Thanks!


          Toby

          Comment

          • Gabriel Genellina

            #6
            Re: Endianness conversion

            En Sat, 24 Feb 2007 14:27:12 -0300, Toby <etatoby@gmail. comescribió:
            I ended up writing my own byteswapper in Pyrex:
            You can use the byteswap method of arrays:
            >>import array
            >>a = array.array('H' , 'ABcd56')
            >>a.tostring( )
            'ABcd56'
            >>a.byteswap( )
            >>a.tostring( )
            'BAdc65'

            --
            Gabriel Genellina

            Comment

            • Dan Sommers

              #7
              Re: Endianness conversion

              On Sat, 24 Feb 2007 17:27:12 +0000, Toby wrote:
              Dan Sommers wrote:
              >You could try the struct module. If your input comes in fixed sized
              >chunks, just call struct.unpack and struct.pack once per chunk.
              >
              Thanks, but it was a bit awkward to use for big chunks.
              def swapper( bytestring ):
              someHs = len( bytestring ) / 2 * "H" # could check for odd-lengths?
              unpackfmt = "<" + someHs
              packfmt = ">" + someHs
              return struct.pack( packfmt, *struct.unpack( unpackfmt, bytestring )

              --
              Dan Sommers
              <http://www.tombstoneze ro.net/dan/>
              Atoms are not things. -- Werner Heisenberg.

              Comment

              Working...