Reputation: 3
I'm just trying to get an 8 BYTE REAL floating-point number from a BYTE array, which I've previously generated from the contents of a file, as a floating-point number.
At the moment I know that an 8 BYTE Real is the same as a double. Therefore, it would have to be logically possible to read out the 8 bytes and assign them directly to the variable. Unfortunately it does not work as I imagine.
As an illustration:
My Byte Array Array for control output of the HEX value stored in the file.
uint64_t result = 0;
for (int i = 0; i < 8; i++){
result = file_buf[content + i];
wsprintf(Outbuf + i*2, L"%02X", result);
OutputMessage(Outbuf, 0);
}
Returns the correct Hex value from the file in the output
C1D6D420937EE766
wsprintf has to use it because I use an API work and rely on a whitecharbuffer.
So far I have tried the following:
resdouble = (double)*&file_buf[content];
swprintf(Outbuf, 200, L" Typ: REAL (8-byte): %Lf\n", resdouble);
OutputMessage(Outbuf, 0);
Outpout:
Typ: REAL (8-byte): 193,000000
An other try:
result = file_buf[content]<<24;
result = result + (file_buf[content + 1] << 16);
result = result + (file_buf[content + 2] << 8);
result = result + ((file_buf[content + 3]));
result = result << 32;
result = result + (file_buf[content + 4] << 24);
result = result + (file_buf[content + 5] << 16);
result = result + (file_buf[content + 6] << 8);
result = result + (file_buf[content + 7]);
memcpy(&resdouble, &result, 8);
wsprintf(Outbuf, L"HEX Result 8 Byte %16X", resdouble);
OutputMessage(Outbuf, 0);
swprintf(Outbuf, 200, L" Typ: REAL (8-byte): %Lf\n", resdouble);
OutputMessage(Outbuf, 0);
Output:
HEX Result 8 Byte 937EE766
Typ: REAL (8-byte): -1532001869,982873
Right result from page: http://www.binaryconvert.com/result_double.html?hexadecimal=C1D6D420937EE766
but would have to come out -1,532002893982. The comma is calculated incorrectly.
Why do I get out the wrong double number and what do I have to do to get the result -1,532001 and not -1532001869,982873 ?
EDIT: I try this from @ Gerhardh
(double)(*&file_buf[content]);
resdouble = *(double*)&file_buf[content];
swprintf(Outbuf, 200, L"Typ: REAL:\t%f\n", resdouble);
OutputMessage(Outbuf, 0);
swprintf(Outbuf, 200, L" Typ: HEX (8-byte): %X\n", resdouble);
OutputMessage(Outbuf, 0);
Output:
Typ: REAL: 5111310630843501598..........
Datentyp: HEX (8-byte): 20D4D6C1
with byteswap in the first line
_byteswap_uint64(file_buf[content]);
i have the same result.
Upvotes: 0
Views: 121
Reputation: 25286
You must cast ergebnis
to a byte aray and then fill each byte. Once done, you have your uint64. For example:
union {
unsigned char b[8];
uint64_t d;
} ergebnis;
for (int i = 0; i < 8; i++){
ergebnis.b[i] = file_buf[inhalt + i];
}
wsprintf(Ausgabebuf + i*2, L"%02X", ergebnis.d);
Also note the "endianness" of your system, or you may have to invert the loop
Upvotes: 1