Sonic and Sega Retro Message Board: Reading binary values C++ - Sonic and Sega Retro Message Board

Jump to content

Hey there, Guest!  (Log In · Register) Help
Page 1 of 1
    Locked
    Locked Forum

Reading binary values C++ float values are wrong

#1 User is offline Alriightyman 

Posted 03 October 2014 - 11:35 AM

  • !!!!!!!!!!!!!!!!!
  • Posts: 351
  • Joined: 27-November 07
  • Gender:Male
  • Location:Largo, Fl
  • Project:0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
  • Wiki edits:5
Writing float data to a binary file and then reading it.

http://pastebin.com/EZxWqKH8
And here is the output:
1.01234        1.01234
2.01234        0.012343
3.01234        0.012344
4.01234        0.012345
5.01234        0.01234



EDIT: Serious Fromating issues
This post has been edited by Alriightyman: 03 October 2014 - 12:32 PM

#2 User is offline GerbilSoft 

Posted 03 October 2014 - 11:46 AM

  • RickRotate'd.
  • Posts: 2846
  • Joined: 11-January 03
  • Gender:Male
  • Location:USA
  • Project:Gens/GS
  • Wiki edits:5,000 + one spin
The output file is:

00000000  31 2e 30 31 32 33 34 32  2e 30 31 32 33 34 33 2e  |1.012342.012343.|
00000010  30 31 32 33 34 34 2e 30  31 32 33 34 35 2e 30 31  |012344.012345.01|
00000020  32 33 34                                          |234|


You're printing the string representations of the floating-point numbers without separators. cin has no idea how to separate them, so it gets the values wrong.

You need to add spaces between the numbers, either an actual space character or a newline.

If you wanted to print the binary representation, you'll need to print the raw bytes, which is more easily done using C stdio than with iostream. (Note that this may cause endian issues if you switch between little-endian and big-endian systems.)

#3 User is offline Alriightyman 

Posted 03 October 2014 - 12:56 PM

  • !!!!!!!!!!!!!!!!!
  • Posts: 351
  • Joined: 27-November 07
  • Gender:Male
  • Location:Largo, Fl
  • Project:0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
  • Wiki edits:5
Thanks, but why is it writing the string representation of the float value? I thought it should write it as a 32-bit binary.

#4 User is offline flamewing 

Posted 03 October 2014 - 02:21 PM

  • Emerald Hunter
  • Posts: 1138
  • Joined: 11-October 10
  • Gender:Male
  • Location:🇫🇷 France
  • Project:Sonic Classic Heroes; Sonic 2 Special Stage Editor; Sonic 3&K Heroes (on hold)
  • Wiki edits:12
The >> and << operators are for formatted input and output; it converts the values passed into strings based on one of a dozen overloads.

What you want is to either use the read or write member functions of the streams; e.g.:
    file.write(reinterpret_cast<char const *>(&outArray[I]), sizeof(outArray[I]));

Edit: But as GerbilSoft mentions, this can lead to big- versus little-endian issues.

Edit 2: Additional reading: output, input.
This post has been edited by flamewing: 03 October 2014 - 02:24 PM

#5 User is offline Alriightyman 

Posted 03 October 2014 - 09:38 PM

  • !!!!!!!!!!!!!!!!!
  • Posts: 351
  • Joined: 27-November 07
  • Gender:Male
  • Location:Largo, Fl
  • Project:0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
  • Wiki edits:5
Thanks a lot! That helped.

I'm not worried about big-/little-endian issues for this little project, but I will keep it in mind.

Page 1 of 1
    Locked
    Locked Forum

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users