0

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.

Sumsum
  • 3
  • 2
  • In the loop: `ergebnis = file_buf[inhalt + i];` just assigns over and over again to `ergebnis`. It does not convert the byte array to a double. – Paul Ogilvie Aug 08 '18 at 12:40
  • You might want to use English names for identifiers. That would make understanding your code easier for non-German speakers. – Adrian W Aug 08 '18 at 12:41
  • FAQ: [What is the strict aliasing rule?](https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule). – Lundin Aug 08 '18 at 12:43
  • Try this one: `resdouble = (double)*&file_buf[content];` => `resdouble = *(double*)&file_buf[content];` – Gerhardh Aug 08 '18 at 13:25

1 Answers1

1

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

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • Thankyou for your answer. Yes, your solution is the better way for the control output for the HEX value. But this is not my problem, My problem is the wrong real value in the resdouble. – Sumsum Aug 08 '18 at 13:15