mardi 24 février 2015

serializing structure


I need to serialize my structure as binary stream. Here is how this struct looks like and how i'm trying to serialize it. I think I receive wrong values in score of my serialization. Could someone looks on it and confirm my way should work?



struct Termometer {
public:
byte* address;
uint32_t name;
float valueC;
uint64_t time;

ContentPair ToFlat() {
uint32_t objSize = sizeof(name) + sizeof(valueC) + sizeof(time);
byte* bytes = new byte[objSize];

uint32_t* namePtr = (uint32_t*) &bytes[0];
namePtr[0] = name;

float* valuePtr = (float*)&bytes[4];
valuePtr[0] = valueC;

uint64_t* timePtr = (uint64_t*)&bytes[8];
timePtr[0] = time;

ContentPair pair;
pair.rawBytes = (void*) bytes;
pair.size = objSize;
return pair;
}
};


With this values:



Termometer t;
t.valueC = 'A';
t.time = 'B';
t.name = 'C';
ContentPair content = t.ToFlat();
content.Print();


it gives me :



C000000BB0000000


this is struct ContentPair:



struct ContentPair
{
void* rawBytes;
uint32_t size;

void Print() {
char* bytes = (char*)rawBytes;
Serial.print("Size is: ");
Serial.println(size);
for(int i = 0; i < size; ++i)
{
if(bytes[i] == 'A' || bytes[i] == 'B' || bytes[i] == 'C')
Serial.print(bytes[i]);
else
Serial.print('0');
}
Serial.println();
}
};



Aucun commentaire:

Enregistrer un commentaire