467,161 Members | 843 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,161 developers. It's quick & easy.

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.
import socket
import struct
output = struct.pack('h', socket.htons(len('12345678')))
output '\x00\x08' output = struct.pack('h', socket.htons(len('123456789')))
output '\x00\t' output = struct.pack('h', socket.htons(len('1234567890')))
output '\x00\n' output = struct.pack('h', socket.htons(len('12345678901')))
output '\x00\x0b'


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.
Jul 18 '05 #1
  • viewed: 3406
Share:
3 Replies

"Gordon Scott" <gs********@hotmail.com> wrote in message
news:D1********************@news3.news.adelphia.ne t...
why in this example are the packing of 9 and 10 not showing '\x00\x09' and
'\x00\x0a' respectively?
It's the canonical representation of that string, after round tripping the
escaping/unescaping. No different to:
('\x41')

'A'
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.


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.
Jul 18 '05 #2
Gordon Scott fed this fish to the penguins on Thursday 02 October 2003
06:49 am:

'\x00\x08'
output = struct.pack('h', socket.htons(len('123456789')))
output '\x00\t' output = struct.pack('h', socket.htons(len('1234567890')))
output '\x00\n' output = struct.pack('h', socket.htons(len('12345678901')))
output '\x00\x0b'

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

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.
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 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).

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #3
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.******@rl.ac.uk> wrote in message
news:bl********@newton.cc.rl.ac.uk...

"Gordon Scott" <gs********@hotmail.com> wrote in message
news:D1********************@news3.news.adelphia.ne t...
why in this example are the packing of 9 and 10 not showing '\x00\x09' and
'\x00\x0a' respectively?
It's the canonical representation of that string, after round tripping the
escaping/unescaping. No different to: ('\x41') 'A'
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.


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.

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Graham Nicholls | last post: by
7 posts views Thread by i_vincent@hotmail.com | last post: by
2 posts views Thread by Bryan Parkoff | last post: by
6 posts views Thread by chris | last post: by
7 posts views Thread by Louis B. (ldb) | last post: by
reply views Thread by Keith | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.