473,587 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pack struct in C for non-aligned data?

In the following structure, bytesPerSector is not aligned to the sizeof(short), so after I read the raw data into buffer, and try to extract the bytesPerSector portion, I got the wrong number. Is there a easy fix for this? I am using Visual C++ Express on a x86 processor.

Expand|Select|Wrap|Line Numbers
  1. struct BS_Structure {
  2.     unsigned char     jumpBoot[3];
  3.     unsigned char     OEMName[8];
  4.     unsigned short    bytesPerSector;
  5.     unsigned char     sectorPerCluster;
  6. } ptr;
  7.  
  8. unsigned char buffer[512];
  9. unsigned short bytesize;
  10.  
  11. ReadSDCard (buffer);
  12. ptr = (BS_Structure*) buffer;
  13. bytesize = ptr->bytesPerSector;
  14.  
Nov 19 '10 #1
2 2757
donbock
2,426 Recognized Expert Top Contributor
bytesPerSector should indeed be properly aligned. What makes you think is isn't? An alignment problem will typically show up with a run-time error message, not improper operation.

Are you sure you don't overflow the bounds of the jumpBoot or OEMName arrays?
Nov 19 '10 #2
newb16
687 Contributor
If the specification of the structure says something like 'on position 11 and 12 there is a 16-bit value'( yes it does - http://en.wikipedia.org/wiki/Fat16#Boot_Sector ) you need to manually extract them from the raw data, like
unsigned short bytesPerSector = raw[12] << 8 | raw[11];
Hoping that a compiler propery align it is not reliable. Endianness also matters.
Nov 20 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3654
by: Joshua Forgione | last post by:
Hello. I am reverse engineering some Python Code. I am using the book "The Quick Python Book" as an aide. The following line exists in the code: msg = struct.pack('BBBB',word0, word1, word2, word3, word4) In this struct.pack command, the format string is 'BBBB.' In the book, it gives the following examples for characters that are...
8
3548
by: Grant Edwards | last post by:
Perhaps I'm doing something wrong: the struct module docs say it's IEE 754, but I can't figure out how to get it to handle NaN values correctly (either packing or unpacking). >>> x = float('nan') >>> struct.pack("<f",x) Traceback (most recent call last): File "<stdin>", line 1, in ? SystemError: frexp() result out of range
3
2695
by: Chandu | last post by:
In using the following struct format I get the size as 593. The same C struct is 590 if packed on byte boundary and 596 when using pragma pack(4). I am using pack(4) and added 3 spares at the end to get by. hdrFormat = '16s 32s 32s B 8s H 8s H 4s H H 20s 64s 64s 64s 32s 32s 64s L L B B B B B 64s 64s' Any ideas on what I am doing wrong?
1
2337
by: Alex Stapleton | last post by:
from struct import pack >>> pack("B", 1) '\x01' >>> pack("BB", 0, 1) '\x00\x01' >>> pack("BI", 0, 1) '\x00\x00\x00\x00\x01\x00\x00\x00' >>> calcsize("BI") 8 >>> calcsize("BB")
3
1463
by: GeLLcheN | last post by:
Hello everybody, I've got a little (endian) problem. I'm programming a network-based application, in which I'll use structs, interpreting ethernet packets. My intention is to read the stream directly and transport to the right struct. But this seems to be not so simple: Here's a sample code demonstrating my problem:
2
1687
by: rong.xian | last post by:
Before I was bitten by the difference below, I think these two ways are the same. However, they are not. Is there any geek who can tell me if this is a bug? (some weird '\x00\x00' was inserted between '0123456789abcd' and 6 ) '\x01\x00\x00\x00%\x910\x01\x0e\x00\x00\x000123456789abcd\x00\x00\x06\x00\x00\x00'...
2
10621
by: Jansson Christer | last post by:
Hi all, I have discovered that in my Python 2.4.1 installation (on Solaris 8), struct.pack handles things in a way that seems inconsistent to me. I haven't found any comprehensible documentation over known issues with Python 2.4.1 so I try this... Here's the thing:
18
7551
by: p.lavarre | last post by:
Can Python not express the idea of a three-byte int? For instance, in the working example below, can we somehow collapse the three calls of struct.pack into one? 08 12 34 56 80 00 I ask because I'm trying to refactor working code that is concise: 08 12 34 56 80 00
2
971
by: =?Utf-8?B?S2V2?= | last post by:
Since installing Service Pack 2 my computer will not recognise my HP all in one printer. Tried uninstalling/reinstalling printer soft ware; disabling security firewall; changing usb cables but no change. I'm using XP home edition and had this same problem previously and only resolved it but deleting SP2 but with Sky internet package SP2 is...
4
2457
by: Joshua J. Kugler | last post by:
I'm trying to put some values into a struct. Some of these values are NaN and Inf due to the nature of the data. As you well may know, struct (and other things) in Python <= 2.4 doesn't support inf and nan float values. You get the dreaded "SystemError: frexp() result out of range" error. Before I go and write my own little wrapper, has...
0
7843
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...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
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...
0
6619
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...
0
5392
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...
0
3840
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...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
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
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.