472,356 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,356 software developers and data experts.

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
3 3564

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Graham Nicholls | last post by:
Hi, I'm trying to size a jpeg file. The file size is held in a short (2 byte integer) at a certain offset. Once I've found these two bytes (they're in MSB,LSB order), I need to convert them to...
2
by: James S | last post by:
Hi, Basically I've been fighting with this code for a few days now and can't seem to work around this problem. Included is the output, the program I use to get this error and the source code for...
5
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
7
by: i_vincent | last post by:
Hi all, Newbie Python programmer here, so please be patient. I have spent all day googling for an answer to my problem, but everything I try fails to work (or works from the Interpreter with a...
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
6
by: chris | last post by:
This is my first attempt at undertaking a C extension module. I want to wrap an existing C library so I can call the functions from Python. There are only two functions I'm interested in calling. ...
6
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
7
by: Louis B. (ldb) | last post by:
I have a long running program that eventually crashes when valloc() returns a 0. This program is relatively non-trivial as it's written in Ada, is multithreaded, has alot of SSE routines. A memory...
0
by: Keith | last post by:
Hello, I am trying to create exectuables on inux using "pyinstaller". I am using pyinstaller-1.3, RHEL 4.4, Python 2.5. The executables fail to run. The problem returned is pertaining to...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.