# Serialize - data packing

This topic is 4490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

let's say i have 2 floats. i want to pack them into one big unit (not an array or vector).

##### Share on other sites
struct float2
{
float f1,f2;
};

Where is the problem??

##### Share on other sites
Perhaps you're looking for a double? Set the high 4 bytes to the first float and the next 4 low bytes to the second?

##### Share on other sites
dbzprogrammer how to do this?

##### Share on other sites
c/c++ :
union U{	struct {		float f1,f2;	};	double d;};...U u;u.f1 = your_float1;u.f2 = your_float2;your_double = u.d;

##### Share on other sites
I understood the concept, and after a quick pull, I double checked my mental code.

Fundamentals, bits are what makes a byte. The higher the bit in the sequence, the greater the number it represents. Each bit represents a 2^x number. Normally, we would copy byte for byte, using the & (bit-wise AND) with 255 to determine which bits we want. However, we're doing 4 bytes at a time, so we will have to use the number 4294967295. This sets 32 bits to '1's.

double PackFloats(float f1, float f2){  return ((double)f1 << 32) + ((double)f2);}

EDIT: Hit the button too soon. The << is an operator that "shifts" bits. Basically, you're telling the computer that you want to fill in 4 bytes of the double with f1, starting at bit 32, and ends up at bit 63, the last bit (remember, we have bit 0 that makes it a total of 64 bits).

##### Share on other sites
Um, packing two small units into a meaningful larger unit only works with unsigned integers. The last bit of code gives you a structure of either two floats or a double.

u.f1 = your_float1;
u.f2 = your_float2;
your_double = u.d;

Is basically filling your_double with meaningless bits.

The
struct float2{
float f1,f2;
}
is most likely what the OP is looking for.

##### Share on other sites
Quote:
 Original post by CocalusUm, packing two small units into a meaningful larger unit only works with unsigned integers. The last bit of code gives you a structure of either two floats or a double.u.f1 = your_float1;u.f2 = your_float2;your_double = u.d;Is basically filling your_double with meaningless bits.The struct float2{ float f1,f2;}is most likely what the OP is looking for.

He has a good point, as floating point precision would get messed up...

But he's just trying to back it up so he could extract it later, would that affect it?

##### Share on other sites
Let me add to my previous post here.

How would you de-searilize it?

With a function like the one before, except those numbers that I showed you would actually come into play. Observe:

struct MyStruct{  float f1;  float f2;}MyStruct ExtractDouble(double tempD){  MyStruct returnS;  returnS.f2 = tempD & 4294967295;  returnS.f1 = (tempD >> 32) & 4294967295;  return returnS;}

##### Share on other sites
Quote:
 Original post by dbzprogrammerdouble PackFloats(float f1, float f2){ return ((double)f1 << 32) + ((double)f2);}

This works with interegs but not with doubles:
error C2296: '<<' : illegal, left operand has type 'double'

1. 1
Rutin
25
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 9
• 9
• 46
• 41
• 23
• ### Forum Statistics

• Total Topics
631749
• Total Posts
3002051
×