- Byte chunk;
- // write from left to right, from least to most significative bit
- for (size_t i = 0; i < bytesCount; i++)
- {
- chunk = (integer >> (i << 3)) & 0xFF;
- buffer[i] = chunk;
- }
+ union {
+ uint32_t i;
+ char bytes[4];
+ } u;
+ u.i = x;
+ for (size_t i = 0; i < 4; i++)
+ buffer[i] = u.bytes[i];
+}
+
+//WARNING: assuming float is 32bits...
+// convert 4-bytes binary float to float
+float bReal_to_double(Byte* pFloat)
+{
+ float res;
+ memcpy(&res, pFloat, 4);
+ return res;
+}
+
+//WARNING: assuming float is 32bits...
+// serialize double with a NON-portable bytes order
+void write_real(float x, Byte* buffer)
+{
+ union {
+ float d;
+ char bytes[4];
+ } u;
+ u.d = x;
+ for (size_t i = 0; i < 4; i++)
+ buffer[i] = u.bytes[i];