473,499 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serializing bit field structures

I have a struct declared as follows:

struct RecordType1
{
unsigned int dt : 24; //3 bytes
unsigned int ts : 16; //2 bytes
unsigned int lsp : 24; //3 bytes (float value represented as int)
unsigned int lst : 16; //2 bytes
unsigned int lsv : 16; //2 bytes
unsigned int x1 : 24; //3 bytes (float value represented as int)
unsigned int x2 : 24; //3 bytes (float value represented as int)
unsigned int x3 : 24; //3 bytes (float value represented as int)
unsigned int x4 : 24; //3 bytes (float value represented as int)
unsigned int bv : 16; //2 bytes
unsigned int ak : 24; //3 bytes (float value represented as int)
unsigned int av : 16; //2 bytes
unsigned int cv : 24; //3 bytes
};

I need to serialize this struct by packing the bits into a contiguous
byte array, and then read it back from the byte array. I cant use
memcpy/sizeof because of boundary alignment ...

I'd appreciate if anyone can show me how to do this. Ieally, I would
like to this in a cross platform (i.e. "ENDIAN-ness" agnostic) way.
Oct 21 '08 #1
2 4917
On 22 Okt., 10:03, James Kanze <james.ka...@gmail.comwrote:
[...] since generally the compiler won't allocate a bit field
in a way that would cross a 32 bit boundary. [...] But there's
rarely any sense in having bit fields larger than 8 bits.)
shure? consider:

typedef unsigned __int64 QUAD;

#pragma pack (push, 1)
struct foo {
union {
QUAD qData;
struct {
QUAD nFirstNibble : 4;
QUAD nSecondNibble : 4;
QUAD nThirdNibble : 4;
QUAD nBloodyRest : 54;
};
};
};
#pragma pack (pop)
Oct 22 '08 #2
On 22 Oct, 17:31, ".rhavin grobert" <cl...@yahoo.dewrote:
On 22 Okt., 10:03, James Kanze <james.ka...@gmail.comwrote:
[...] since generally the compiler won't allocate a bit field
in a way that would cross a 32 bit boundary. [...] But there's
rarely any sense in having bit fields larger than 8 bits.)

shure? consider:

typedef unsigned __int64 QUAD;
eek! I suppose since you've hidden __int64 behind
a typedef you can live with that

#pragma pack (push, 1)
eek! ** 2

the guy want's *portable* stuff! How many platforms
does this work on?

struct foo {
* union {
* * QUAD qData;
* * struct {
* * * QUAD nFirstNibble *: *4;
* * * QUAD nSecondNibble : *4;
* * * QUAD nThirdNibble *: *4;
* * * QUAD nBloodyRest * : 54;
* * };
* };};

#pragma pack (pop)
--
Nick Keighley

Oct 23 '08 #3

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

Similar topics

3
1561
by: Philippe C. Martin | last post by:
Hi, I am trying to figure out the traps/issues about sending data structures through a TCP/IP socket under the following circumstances: 1) there could be a different O/S on both side of the...
11
3438
by: Bradford Chamberlain | last post by:
I work a lot with multidimensional arrays of dynamic size in C, implementing them using a single-dimensional C array and the appropriate multipliers and offsets to index into it appropriately. I...
10
8246
by: copx | last post by:
I want to save a struct to disk.... as plain text. At the moment I do it with a function that just writes the data using fprintf. I mean like this: fprintf(fp, "%d %d", my_struct.a, my_struct.b)...
5
4763
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e....
13
5206
by: Leszek Taratuta | last post by:
Hello, I have several drop-down lists on my ASP.NET page. I need to keep data sources of these lists in Session State. What would be the most effective method to serialize this kind of data...
1
2261
by: Farouche | last post by:
Hi all I have made two simple methods to serialize/deserialize a simple data structure to my database using SoapFormatting. This actually works just great, when the structures stays the same....
2
331
by: Charles Law | last post by:
I have a complex object that I am serializing, and I would like to omit elements that have a default value. For example, if I have a class as follows Public Class Test Private m_Name As...
12
4884
by: Cagdas Ozgenc | last post by:
Greetings, When directly serializing C++ structures to a file with the standard library functions giving the address of the data and length of structure using the sizeof operator, do I risk...
4
1463
by: colin | last post by:
How can I access a field contained in fieldInfo by reference so I can pass it to a function ? ive tried __refvalue but this needs a type known at compile time, I can access it with SetValue and...
0
7131
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
7174
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
7220
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...
1
6894
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
5470
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,...
1
4919
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...
0
4600
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...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
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...

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.