Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3460658
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:15:23+00:00 2026-05-18T10:15:23+00:00

There are some discussions about the same question but I would like to ask

  • 0

There are some discussions about the same question but I would like to ask some more ,

1) How portable is the below code for a double byte swapping

int ReadDouble(FILE *fptr,double *n)
{
   unsigned char *cptr,tmp;

   if (fread(n,8,1,fptr) != 1)
      return(FALSE);

   cptr = (unsigned char *)n;
   tmp = cptr[0];
   cptr[0] = cptr[7];
   cptr[7] = tmp;
   tmp = cptr[1];
   cptr[1] = cptr[6];
   cptr[6] = tmp;
   tmp = cptr[2];
   cptr[2] = cptr[5];
   cptr[5] =tmp;
   tmp = cptr[3];
   cptr[3] = cptr[4];
   cptr[4] = tmp;

   return(TRUE);
} 

2) Should I keep the 3 important parts of a floating point number, sign bit, mantissa, exponent as integers and then try to manipulate them somehow.

I know the basics of floating point representations, not that deeply as a mechanical engineer, however I need to read some big-endian file where my machine is little endian. I can maybe worry about the portability issues later on. But I would like to learn about them perhaps you can direct me to some more direct things on this because there is too much information on this, I was confused which one to read.

So after some comments this should more or less do that in a portable way right? Sorry for the C file pointers…

double_t ReadDouble(ifstream& source) {
  // read 
  char buf[sizeof(double_t)];
  source.read(buf, sizeof(double_t));
  // reverse and return
  reverse( buf, buf+sizeof(double_t) );
  return *(reinterpret_cast<double_t*>(buf));
}

Best,
Umut

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:15:24+00:00Added an answer on May 18, 2026 at 10:15 am

    It’s not as easy as that. Just because an architecture is big-endian for integers doesn’t mean it’s big-endian for floating point numbers. I’ve heard of platforms that store integers big-endian and floats little-endian.
    So first you should discover what the actual memory representation of double on your source platform is.

    As for the swap itself, it’s inefficient and way too much code. An additional 8-byte buffer won’t kill you, so why not do this:

    int ReadDouble(FILE* f, double* n) {
      unsigned char* nbytes = reinterpret_cast<unsigned char*>(n);
      unsigned char buf[sizeof(double)];
      if (fread(buf, sizeof(double), 1, f) != 1) return FALSE;
    
      for (int i = 0; i < sizeof(double); ++i) {
        nbytes[i] = buf[sizeof(double)-1-i];
      }
      return TRUE;
    }
    

    Way less code, even if you decide to manually unroll the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are already some discussions here on stackoverflow about Java generics, but I am
There are many discussions on this topic already, but I am all about flogging
This is not a question specific about some javascript detail but I'm looking for
There are some discussions about the problem associated with MongoDB caching in OpenVZ, I
There's been a discussion between me and some colleagues that are taking the same
Although I found many answers and discussions about this question, I am unable to
There are some situations where there are multiple methods to calculate the same value.
There are some related questions here and here , but they didn't really give
I would like to define some properties in my logback.xml config file and saw
There have been discussions about how to compare two images, calculate the difference between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.