Why is floating-point byte change different from byte exchange by integer byte?

I have a doubles binary that I need to load using C ++. However, my problem is that it was written in capital letter format, but the fstream → operator then reads the number incorrectly, because my machine is insignificant. Apparently, a simple task for solving integers, but for paired and floating solutions that I found will not work. How can I (or should I) fix this?

I read this as a reference for integer byte exchange:
How to convert big-endian and little-endian values ​​to C ++?

EDIT: Although these answers are enlightening, I found that my problem is with the file itself and not with the binary data format. I believe my byte exchange does the work, I just get confusing results. Thank you for your help!

+3
source share
2 answers

- , . operator>>, >>. ( ) , , . . , , .

IEEE-754 , , , ( ).

, , , (.. ) unsigned char, , .

+6

, , , .

, , , , , , .

, , .

int32_t raw_bytes;
stream >> raw_bytes; // not an int, just 32 bits of bytes
my_byte_swap( raw_bytes ); // swap 'em
float f = * reinterpret_cast< float * >( & raw_bytes ); // read them into FPU
0

Source: https://habr.com/ru/post/1743119/


All Articles