Need help with struct module

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

    Need help with struct module

    Hi All,

    I've got a problem I'm seeing when trying to use the struct module to send
    data to a different machine.
    Actually I'm making a condensed file that gets transferred to and read on a
    BREW enabled cell-phone,
    essentially I'm trying to format content as if it were being streamed over a
    socket.

    So I am trying to write a string by first sending a two-byte length of the
    string followed by the string itself.
    I noticed what looks to me like strange behavior in the struct module.

    I am encoding the lengths of the strings by using socket.htons and then
    packing them in a string as follows:

    ActivePython 2.2.2 Build 224 (ActiveState Corp.) based on
    Python 2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)] on win32
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> import socket
    >>> import struct
    >>> output = struct.pack('h' , socket.htons(le n('12345678')))
    >>> output[/color][/color][/color]
    '\x00\x08'[color=blue][color=green][color=darkred]
    >>> output = struct.pack('h' , socket.htons(le n('123456789')) )
    >>> output[/color][/color][/color]
    '\x00\t'[color=blue][color=green][color=darkred]
    >>> output = struct.pack('h' , socket.htons(le n('1234567890') ))
    >>> output[/color][/color][/color]
    '\x00\n'[color=blue][color=green][color=darkred]
    >>> output = struct.pack('h' , socket.htons(le n('12345678901' )))
    >>> output[/color][/color][/color]
    '\x00\x0b'[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    why in this example are the packing of 9 and 10 not showing '\x00\x09' and
    '\x00\x0a' respectively?

    On the target device when I read the file into a memory buffer and examine
    the contents,
    the value of 10 ('\x00\n') that I am expecting is litterally 00 0D and NOT
    00 0A. When I encode a longer string
    in this maner such as length 12 or length 15, or shorter strings like length
    7, everything works okay.

    Any ideas?

    PS. reversing the above steps in python correctlly returns 9 and 10.


  • Richard Brodie

    #2
    Re: Need help with struct module


    "Gordon Scott" <gscott2112@hot mail.com> wrote in message
    news:D1Web.6875 $YO5.4091894@ne ws3.news.adelph ia.net...
    [color=blue]
    > why in this example are the packing of 9 and 10 not showing '\x00\x09' and
    > '\x00\x0a' respectively?[/color]

    It's the canonical representation of that string, after round tripping the
    escaping/unescaping. No different to:[color=blue][color=green][color=darkred]
    >>> ('\x41')[/color][/color][/color]
    'A'
    [color=blue]
    > On the target device when I read the file into a memory buffer and examine
    > the contents, the value of 10 ('\x00\n') that I am expecting is litterally 00 0D
    > and NOT 00 0A.[/color]

    I guess at some point you've read/written/transferred the file without the binary flag.
    Try dumping the file on each platform to see where it got converted. Then go back
    and add a few '\x62's.


    Comment

    • Dennis Lee Bieber

      #3
      Re: Need help with struct module

      Gordon Scott fed this fish to the penguins on Thursday 02 October 2003
      06:49 am:

      [color=blue]
      > '\x00\x08'[color=green][color=darkred]
      >>>> output = struct.pack('h' , socket.htons(le n('123456789')) )
      >>>> output[/color][/color]
      > '\x00\t'[color=green][color=darkred]
      >>>> output = struct.pack('h' , socket.htons(le n('1234567890') ))
      >>>> output[/color][/color]
      > '\x00\n'[color=green][color=darkred]
      >>>> output = struct.pack('h' , socket.htons(le n('12345678901' )))
      >>>> output[/color][/color]
      > '\x00\x0b'[color=green][color=darkred]
      >>>>[/color][/color]
      >
      > why in this example are the packing of 9 and 10 not showing '\x00\x09'
      > and '\x00\x0a' respectively?
      >[/color]
      The binary data /is/ what you expect. The string representation is
      being simplified to the escape codes one would normally find in a (user
      entered) string for <tab> and <newline>(aka <lf>) -- which have the
      values of 9 and 10.
      [color=blue]
      > On the target device when I read the file into a memory buffer and
      > examine the contents,
      > the value of 10 ('\x00\n') that I am expecting is litterally 00 0D and
      > NOT[/color]
      Looks like a text mode transfer converting from a system that uses
      <lf> to the format of a system that uses <cr>.

      If you have the control for it, try performing the transfer with the
      "file" in binary mode (on both ends).

      --[color=blue]
      > =============== =============== =============== =============== == <
      > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
      > wulfraed@dm.net | Bestiaria Support Staff <
      > =============== =============== =============== =============== == <
      > Bestiaria Home Page: http://www.beastie.dm.net/ <
      > Home Page: http://www.dm.net/~wulfraed/ <[/color]

      Comment

      • Gordon Scott

        #4
        Re: Need help with struct module

        hmmm, I'll try that.

        I'm not exactlly transferring the file over a socket. Just writing a binary
        file and manually copying it up to the device.
        Did the same thing in Java, dumped a binary file on my desktop and copied it
        over to the device without any problem.

        "Richard Brodie" <R.Brodie@rl.ac .uk> wrote in message
        news:blhc7f$l18 @newton.cc.rl.a c.uk...[color=blue]
        >
        > "Gordon Scott" <gscott2112@hot mail.com> wrote in message
        > news:D1Web.6875 $YO5.4091894@ne ws3.news.adelph ia.net...
        >[color=green]
        > > why in this example are the packing of 9 and 10 not showing '\x00\x09'[/color][/color]
        and[color=blue][color=green]
        > > '\x00\x0a' respectively?[/color]
        >
        > It's the canonical representation of that string, after round tripping the
        > escaping/unescaping. No different to:[color=green][color=darkred]
        > >>> ('\x41')[/color][/color]
        > 'A'
        >[color=green]
        > > On the target device when I read the file into a memory buffer and[/color][/color]
        examine[color=blue][color=green]
        > > the contents, the value of 10 ('\x00\n') that I am expecting is[/color][/color]
        litterally 00 0D[color=blue][color=green]
        > > and NOT 00 0A.[/color]
        >
        > I guess at some point you've read/written/transferred the file without the[/color]
        binary flag.[color=blue]
        > Try dumping the file on each platform to see where it got converted. Then[/color]
        go back[color=blue]
        > and add a few '\x62's.
        >
        >[/color]


        Comment

        Working...