Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Converts Float to Bytes and Bytes to Float;
- union {
- double myDouble;
- unsigned char myChars[ sizeof( double ) ];
- } test;
- // -- Testing Code --
- test.myDouble = 3.14;
- for( int k = 0; k < sizeof( double ); k++ ) {
- std::cout << ( int ) test.myChars[ k ] << ' ';
- }
- cout << endl;
- // Output: 31 133 235 81 184 30 9 64
- test.myChars[ 0 ] = 31;
- test.myChars[ 1 ] = 133;
- test.myChars[ 2 ] = 235;
- test.myChars[ 3 ] = 81;
- test.myChars[ 4 ] = 184;
- test.myChars[ 5 ] = 30;
- test.myChars[ 6 ] = 9;
- test.myChars[ 7 ] = 64;
- cout << test.myDouble << endl;
- // Output: 3.14
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement