Need help with struct module


G

Gordon Scott

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

Advertisements

R

Richard Brodie

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:
'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.
 
D

Dennis Lee Bieber

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

'\x00\x08'

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

--
 
Ad

Advertisements

G

Gordon Scott

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.
 

Top