Trouble reading a binary file as float values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tophandasa
    New Member
    • Mar 2007
    • 15

    #16
    I forget to mention this..
    56E00: 44 3B 92 83 44 5A 6E EA 44 78 82 CF 44 32 E2 B7 "some strange letters and signs here"

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #17
      Originally posted by tophandasa
      I forget to mention this..
      56E00: 44 3B 92 83 44 5A 6E EA 44 78 82 CF 44 32 E2 B7 "some strange letters and signs here"
      Good now you can see what your floating point numbers look like in Hex. Now in order to calculate your floating point numbers you can read this article which will help you understand how floating point numbers are encoded in binary.

      Comment

      • tophandasa
        New Member
        • Mar 2007
        • 15

        #18
        Well, its quite hard to understand, but there should be some conversion functions that i can use here, right? or i'll have to write a function to do this conversion?Plus , what would i have to convert?the Hex values or the strange signs and letters?

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #19
          The strange signs and letters and the hex values are the same thing. You would probably benefit by reading this article.

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #20
            Actually the conversion happens when you read in the binary data from the file stream. You tell the system to read from the file enough data to fill a float. The system reads in the amount of bits the system has defined as float and there you have it.

            Comment

            • tophandasa
              New Member
              • Mar 2007
              • 15

              #21
              So, the question is "How should i tell the system to read the data as float?" knowing that the read function does not deal with float type !!

              Comment

              • horace1
                Recognized Expert Top Contributor
                • Nov 2006
                • 1510

                #22
                Originally posted by tophandasa
                So, the question is "How should i tell the system to read the data as float?" knowing that the read function does not deal with float type !!
                have a look at this simple program which writes 10 float values to a file in binary mode and then reads them back
                Code:
                // write float to a binary file and read them back
                #include <fstream>
                #include <iostream>
                using namespace std;
                
                int main () {
                  float test;
                  fstream outfile;
                  // create file with some data
                  outfile.open ("test.dat",  ios::binary | ios::out);
                    if (! outfile)                                                 // open OK ?
                        {
                        cout << "\nUnable to open file test.dat "  << strerror(errno);
                        cin.get();
                        return 1;                                                 // open failed
                        }
                  // write 10 floats to the file
                  for(int i=0; i< 10; i++)
                      {
                       test=i*1000.;
                       outfile.write ((char *) &test,sizeof(float));
                       }
                  outfile.close();  
                  
                  // now, open file for read/write
                  outfile.open ("test.dat",  ios::binary | ios::out | ios::in);
                  if (! outfile)                                                 // open OK ?
                        {
                        cout << "\nUnable to open file "  << strerror(errno);
                        return 1;                                                 // open failed
                        }
                  // print the contents 
                  cout << "\nfile contents\n";
                  while(outfile.good())
                      {
                       outfile.read((char *) &test,sizeof(float));
                       if(outfile.good()) cout << "test " << test << endl;
                       }
                   outfile.close();
                   system("pause");
                  return 0;
                }

                Comment

                • tophandasa
                  New Member
                  • Mar 2007
                  • 15

                  #23
                  Here is the code im working with now..

                  Code:
                   
                  #include <iostream>
                  #include <iomanip>
                  #include <fstream>
                  
                  using namespace std;
                  
                  int main()
                  {
                      ifstream infile;
                      char *inname = "o1_o2.pwr1";
                  
                  	const int ByteNumber=8346836; 
                  	int count=0;
                      unsigned i;
                      int length;
                  	float x;
                      unsigned char *cXpoint = (unsigned char *)&x;
                  
                      infile.open(inname, ios::binary);
                      if (!infile) {
                          cout << "There was a problem opening file " << inname
                               << " for reading." << endl;
                          return 0;
                      }
                      cout << "Opened " << inname << " for reading." << endl;
                  
                  	// get length of file:
                      infile.seekg (0, ios::end);
                      length = infile.tellg();
                      infile.seekg (0, ios::beg);
                      cout << "The file's length has been determined. \nLength = The number of bytes in the file = " << length << endl;
                  
                  	int j = 0;
                  
                      infile.seekg (ByteNumber);
                  
                  ///	while (!infile.eof())
                  //	{
                  		infile.read((char *)&x, sizeof(float));
                  		if (!infile) {
                  			cout << "There was a problem reading " << sizeof(float) << " bytes from " << inname << endl;
                  			return 0;
                  		}
                  
                  		cout << "The sizeof(float) value is: " << sizeof(float) << endl;  
                  		cout << "The value of x is: " << x << endl;
                  		cout << "Successfully read a float from the file." << endl;
                  		cout << "The count of word now is: " << count << endl; 
                  		cout << "The bytes of x in memory : ";
                  
                  		for (i = 0; i < sizeof(float); i++) 
                  		{
                  			cout << "0x" << hex << setfill('0') << setw(2) << (int)cXpoint[i] << " ";
                  			count++;
                  		}
                  	    
                  		cout << endl;
                  		cout << "x = " << fixed << setprecision(6) << x << endl << endl;
                  
                  //	}
                  
                      return 0;
                  
                  }
                  but the problem is, i have a float value that i should get as the value of a certain 4 byte, i get the right hexadecimal value for these 4 bytes as in the file i have (which i can access now using the TextPad). The value i am getting is 46 C3 74 BF which is not equal to the float value i should get, for these specific bytes, which is 1.359e+4
                  Can it be a conversion problem? If yes, how can i convert/read the hexadecimal value 46 C3 74 BF to get the float value 1.359e+4 ?

                  Thanks a lot for your help :)

                  Comment

                  • horace1
                    Recognized Expert Top Contributor
                    • Nov 2006
                    • 1510

                    #24
                    what type of computer are you using ? on an intel 8086 family using IEEE 574 floating point representation 1.356e+4 would be 46545800 hexadecimal, see


                    If I run the following code
                    Code:
                    union data
                    {
                         unsigned char ch[4];
                         float x;
                    };
                    
                    int main(void)
                    {
                      data a;
                      a.x= 1.359e+4;
                      cout << a.x << endl;
                      for(int i=0; i<4;i++) cout << hex << (int)a.ch[i] << " ";
                    on my PC using g++ I get
                    00 58 54 46
                    being a little endian machine

                    Comment

                    • tophandasa
                      New Member
                      • Mar 2007
                      • 15

                      #25
                      I believe the file was created by a mac or linux OS, while im working now on a windows OS, would it make any difference?My program does produces the value 46 C3 74 BF by the float value -0.956105 but the problem, when i read these certain bytes using the program that has originally created the file im reading now, it displays the float value 1.359e+4 for the same Hex value !!

                      Comment

                      • horace1
                        Recognized Expert Top Contributor
                        • Nov 2006
                        • 1510

                        #26
                        Originally posted by tophandasa
                        I believe the file was created by a mac or linux OS, while im working now on a windows OS, would it make any difference?My program does produces the value 46 C3 74 BF by the float value -0.956105 but the problem, when i read these certain bytes using the program that has originally created the file im reading now, it displays the float value 1.359e+4 for the same Hex value !!
                        x86 PC family are little endian and the power-pc Macs are big endian so this could be part of your problem. see
                        http://www.astro.gla.a c.uk/users/norman/star/sc13/sc13.htx/N-a2b3c2.html

                        however, this does not explain why the hex values are different
                        can you find out what the original machine that generated the file was?

                        Comment

                        • horace1
                          Recognized Expert Top Contributor
                          • Nov 2006
                          • 1510

                          #27
                          Originally posted by horace1
                          what type of computer are you using ? on an intel 8086 family using IEEE 574 floating point representation 1.356e+4 would be 46545800 hexadecimal, see


                          If I run the following code
                          Code:
                          union data
                          {
                               unsigned char ch[4];
                               float x;
                          };
                          
                          int main(void)
                          {
                            data a;
                            a.x= 1.359e+4;
                            cout << a.x << endl;
                            for(int i=0; i<4;i++) cout << hex << (int)a.ch[i] << " ";
                          on my PC using g++ I get
                          00 58 54 46
                          being a little endian machine
                          just ran this on an iMac (power PC) on got
                          46 54 58 00
                          confirms it is bit endian as against the little endian PC

                          Comment

                          • tophandasa
                            New Member
                            • Mar 2007
                            • 15

                            #28
                            Help needed over here guys..Im totally convienced with the results i got, but looks like they are not the results expected by my Professor. I believe he is using other program to retrieve the data in float format. So, he said that the data i got is not the correct data. I dont know what to do now. The results im getting using my C++ program looks okay comparing with the values i get after converting the Hex values read by the TextPad Program from the data file im supposed to read. All he said was, you have to read the data file in binary mode, then read each four bytes into a float variable, and this is what i have done; yet, i cannot get the correct results. What might be the problem..Please Help :(

                            Comment

                            • RedSon
                              Recognized Expert Expert
                              • Jan 2007
                              • 4980

                              #29
                              It sounds like you are going to have to discuss with your professor the point of the project. If there is some particular aspect of programming that you are supposed to learn by doing this assignment then you have to ask your professor to decide if you have in fact learned that or not. If the point of the lesson is to write some code that manipulates some binary data, ask your professor if you have not demonstrated that you can do that.

                              Comment

                              • RedSon
                                Recognized Expert Expert
                                • Jan 2007
                                • 4980

                                #30
                                The point is that there are any number of ways to solve a problem in programming, you can do things a million different ways. Just because you solved a problem differently then what your professor expects to see does not mean that you should be penalized. If however you did not follow some of the project rules then it is your own fault.

                                Comment

                                Working...