473,406 Members | 2,956 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,406 software developers and data experts.

struct is saving 4 bytes instead of 2

Using UBUNTU python 2.4.3 I'm using struct to save a variable to a
file. My goal is to save it as a 2 byte signed integer. Its being
saved using:

f.write(struct.pack('h',thevariable))

When I inspect the file with a hex editor it show that the variable is
being saved as 4 bytes. How can I make it save the value as 2 bytes?

Tony

Sep 12 '07 #1
3 1113
TonyB wrote:
When I inspect the file with a hex editor it show that the
variable is being saved as 4 bytes. How can I make it save the
value as 2 bytes?
Use an aligment specifier in the format string (you want "standard"
alignment). See:

http://docs.python.org/lib/module-struct.html

Regards,
Björn

--
BOFH excuse #428:

Firmware update in the coffee machine

Sep 12 '07 #2
On Sep 12, 6:45 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
TonyB wrote:
When I inspect the file with a hex editor it show that the
variable is being saved as 4 bytes. How can I make it save the
value as 2 bytes?

Use an aligment specifier in the format string (you want "standard"
alignment). See:

http://docs.python.org/lib/module-struct.html

Regards,

Björn

--
BOFH excuse #428:

Firmware update in the coffee machine
Björn,

I tried

f.write(struct.pack('=h',thevariable))

and it works. Many thanks,
Tony

Sep 12 '07 #3
TonyB wrote:
I tried

f.write(struct.pack('=h',thevariable))

and it works.
Great! :)

Regards,
Björn

--
BOFH excuse #45:

virus attack, luser responsible

Sep 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: grant | last post by:
Hi All, I am pretty new to python and am having a problem intepreting binary data using struct.unpack. I am reading a file containing binary packed data using open with "rb". All the values are...
4
by: James Harris | last post by:
Having updated my Debian system it now complains that I am using an incompatible pointer type in warnings such as "passing arg 2 of 'bind' from incompatible pointer type" from code, struct...
4
by: Phui Hock | last post by:
Hi, I'm a newbie in C. Can anyone tell me why the size of 'test' is 8 bytes instead of 4 bytes when I uncomment the 'struct list *next;'? I hope someone can can explain to me. I can't find answer...
8
by: Luc Le Blanc | last post by:
I have 2 APIs that store/recall a void *. Since all I need to store is a 32-bit struct, I pass the actual data (instead of a pointer to it) as a void *: typedef { UInt8 color;...
7
by: ANaiveProgrammer | last post by:
Hi all I have made the following two structs and size is not according to what is supposed to be, so please ponder over following and identify if im wrong... please also mention what would be...
1
by: J | last post by:
I'm interfacing with a C api (via Interop) which uses the following typedef struct... typedef struct _columnflags { BYTE bNoUpdate : 1; BYTE bSetToNull : 1; BYTE bDefault : 1; }...
2
by: Michael Yanowitz | last post by:
Hello: I am relatively new to Python and this is my first post on this mailing list. I am confused as to why I am getting size differences in the following cases: >>> print...
5
by: Hallvard B Furuseth | last post by:
Does struct assignment copy padding bytes? Some compilers do, but I couldn't find anything in the standard which says they must. What I need is for any padding bytes to contan initialized values...
5
by: kevincw01 | last post by:
I've got this homework assignment where I need to send some data over a simulated network. Don't worry, I dont want someone to write the code for me, just wondering if there are problems with the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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...
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...
0
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,...
0
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...

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.