473,395 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 3625

"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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.