Advertisement

10.04.2006 at 12:27AM PDT, ID: 22012167
[x]
Attachment Details

Reading a little endian float on a big endian machine

Asked by riksweeney in C Programming Language

Tags: endian, float, big, little

Hi,

I have a problem reading floats that have been written to a binary file on a little endian machine (x86). When I try to read the file on a big endian machine (PPC), the floats get read in as 0.0000.

Integers get read in correctly and I just need to switch the endian to get the correct value. Here is the code that I'm using

      float v[5];
      int w[5];
      
      FILE *fp = fopen("test.dat", "rb");
      
      if (fp == NULL)
      {
            fp = fopen("test.dat", "wb");
            
            v[0] = 1.2f;
            v[1] = 2.3f;
            v[2] = 3.4f;
            v[3] = 4.5f;
            v[4] = 5.6f;
            
            w[0] = 10;
            w[1] = 20;
            w[2] = 30;
            w[3] = 40;
            w[4] = 50;
            
            fwrite(v, sizeof(float), 5, fp);
            fwrite(w, sizeof(int), 5, fp);
            
            fclose(fp);
      }
      
      else
      {
            fp = fopen("test.dat", "rb");
            
            fread(v, sizeof(float), 5, fp);
            fread(w, sizeof(int), 5, fp);
            
            printf("Got %f %f %f %f %f\n", v[0], v[1], v[2], v[3], v[4]);
            printf("Got %d %d %d %d %d\n", w[0], w[1], w[2], w[3], w[4]);
      }
      
      exit(0);

The first if statement just writes the file if it doesn't exist. The output of the program when run on 68k or PPC is

Got -0.00000 0.00000 -0.00000 0.00000 0.00000
Got 167772160 335544320 503316480 671088640 838860800

Does anyone know what I can do to get the floats to read in correctly so that I can then swap the endian?

Thanks

RichardStart Free Trial
[+][-]10.04.2006 at 04:31AM PDT, ID: 17658469

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.04.2006 at 06:33AM PDT, ID: 17659201

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.04.2006 at 06:50AM PDT, ID: 17659363

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C Programming Language
Tags: endian, float, big, little
Sign Up Now!
Solution Provided By: Exceter
Participating Experts: 1
Solution Grade: B
 
 
[+][-]10.04.2006 at 06:52AM PDT, ID: 17659383

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.04.2006 at 07:26AM PDT, ID: 17659635

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.05.2006 at 12:01AM PDT, ID: 17666388

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32