Advertisement
FatalSleep

Floats To Bytes && Bytes To Floats

Jun 5th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. //Converts Float to Bytes and Bytes to Float;
  2. union {
  3. double myDouble;
  4. unsigned char myChars[ sizeof( double ) ];
  5. } test;
  6.  
  7. // -- Testing Code --
  8. test.myDouble = 3.14;
  9.  
  10. for( int k = 0; k < sizeof( double ); k++ ) {
  11. std::cout << ( int ) test.myChars[ k ] << ' ';
  12. }
  13.  
  14. cout << endl;
  15. // Output: 31 133 235 81 184 30 9 64
  16.  
  17.  
  18. test.myChars[ 0 ] = 31;
  19. test.myChars[ 1 ] = 133;
  20. test.myChars[ 2 ] = 235;
  21. test.myChars[ 3 ] = 81;
  22. test.myChars[ 4 ] = 184;
  23. test.myChars[ 5 ] = 30;
  24. test.myChars[ 6 ] = 9;
  25. test.myChars[ 7 ] = 64;
  26. cout << test.myDouble << endl;
  27. // Output: 3.14
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement