convert 8 bytes into double and back?
I have a question. I receive a byte stream. The first 8 bytes contain an Identification number. I receive at first the lowest byte and at the end the highest byte of the number.
How can I transform this into a double value and later back into the bytestream? In the past I hard only 2 Byte values and there I could use things like MAKEWORD and HIBYTE and LOWBYTE to convert the values
Re: convert 8 bytes into double and back?
if the 8 bytes represent an actualdouble in the double format...
1) use a Union of a double and an array of 8 bytes.
2) store the bytes in an 8 byte array, the copy the contents into a double with memcpy()
3) store in an array, then typecast the array pointer to a double pointer and dereference.
...
now. if the 8 bytes aren't in double format. you'll need to convert, can't help you there without more info.
Re: convert 8 bytes into double and back?
The format of a double is described in
http://en.wikipedia.org/wiki/Double-...g-point_format
Basically there are 52 bits for the fraction part, 11 bits for the exponent and 1 bit for the sign. From the OP post #1, the 8 bytes represent an identification number which is probably an integer and not a floating point (double) number so a double number cannot be used correctly to store the id.
The type you probably need is long long or __int64 (both 8 byte integer).
Re: convert 8 bytes into double and back?
One possible way of obtaining the id as a long long is
Code:
#include <stdio.h>
int main()
{
unsigned char id[] = {1,2,3,4,5,6,7,8};
long long res = 0;
for (int b = 0; b < 8; ++b)
res |= ((long long)id[b]) << (b * 8);
unsigned char *ares = (unsigned char*)&res;
for (int i = 0; i < 8; ++i)
printf("%02x ", ares[i]);
return 0;
}
Re: convert 8 bytes into double and back?
Thank you the last post is helping me. so far I created me this type for my 8 byte value:
typedef unsigned __int64 QWORD;