473,706 Members | 3,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

"Gordon Scott" <gs********@hot mail.com> wrote in message
news:D1******** ************@ne ws3.news.adelph ia.net...
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(le n('123456789')) )
output '\x00\t' output = struct.pack('h' , socket.htons(le n('1234567890') ))
output '\x00\n' output = struct.pack('h' , socket.htons(le n('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.netc om.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.a c.uk...

"Gordon Scott" <gs********@hot mail.com> wrote in message
news:D1******** ************@ne ws3.news.adelph ia.net...
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
11041
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 an integer - now I know that in C I'd just cast a pointer to the offset to a short, and that python doesn't cast, so how can I extract the value from a stream of bytes. I've looked at python.org/Doc/current (I'm using 2.3b1), but can't find...
2
1992
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 my wrapper. This is acually part of the project, libxmlconf on sourceforge. The newest working version isn't there yet, and cvs is lagged by 6 hours or so. So if you think you want to have a try at this I can tgz the source for you. My...
5
13847
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: '\x00\x00\xb9\x02\x13EXCLUDE_CREDIT_CARD' # the above was printed using repr(xbuffer). # Note that int(0x13) = 19 which is exactly the length of the visible text #
7
12237
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 set value but not from my code with dynamic values). Okay, here is the general gist of the problem. I am using Python to parse an output file (from a MAK Logger but that is not really important). Now I have some data that is contained on a line...
2
5187
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 second name for better readable. After optimization, global variables might be misaligned because each global variables must be converted to 32 bits, but I do see that C/C++ Compiler do padding between variables. Struct does the same to do padding....
6
1275
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. I did mess with Pyrex a bit and Swig, to no avail, so I turned to doing it by hand. Using the example in Programming Python, I did get the easier of the two functions working--only takes a string parameter. I'm stuck now on the other function...
6
2879
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 suggestion for improvements or conversion to iteration would be much appreciated
7
2568
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 leak would be the most obvious cause but this appears to be more sinister then a simple memory leak. After alot of running around and searching through the code I found an anomaly that I'd like to explain and understand if it's the cause of...
0
1705
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 "struct.py" not being able to find the module "_struct". struct.py is located under /usr/local/lib/python-2.5/, and there is a _struct.o (no _struct.py anywhere) located under /usr/local/lib/ python-2.5/lib-dynload.
0
8695
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8987
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7900
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6614
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5937
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3142
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2499
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2089
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.