When I need to serialize a double to a byte stream and vice versa I use these functions in C ( and C++)
#SelectExpand
1DoubleToBytes
(double a_dDouble
)
2{
3 std::vector
<uint8_t> vResult
;
4 const unsigned char *pBtyeArray
= reinterpret_cast<const unsigned char*>(&a_dDouble
);
5 const unsigned char *pBtyeArrayEnd
= pBtyeArray
+ sizeof(double) -1;
6
7 for (; pBtyeArray
<= pBtyeArrayEnd
; pBtyeArray
++)
8 {
9 unsigned char b
= *pBtyeArray
;
10
11
12 vResult.push_back
(*pBtyeArray
);
13 }
14
15 return vResult
;
16}
17
18BytesToDouble
(std::vector
<uint8_t> a_zByteArray
)
19{
20 double dNumber
;
21 memcpy(&dNumber, a_zByteArray.data
(),
sizeof(double));
22 return dNumber
;
23}
This works great as long as sender and receiver are programmed in C or C++.
However, in my case right now the receiving end is programmed in C#...::)
And I have a hard time converting the byte array to double again.
Nothing seems to work...
Any Ideas ?