• C

Byte swapping a double in C / C++

Hi, im converting a program from a silicon graphics machine to the PC.

I would like to know how to byte swap a double

I have tried the following but it doesnt seem to work :-

void byteSwapDouble(double *ptrValue)
     double  *ptrTemp;
     char * swapA, swapB;

     ptrTemp = ptrValue;
     swapA = (char *)ptrTemp;

     swapB = swapA[0];
     swapA[0] = swapA[7];
     swapA[7] = swapB;
     swapB = swapA[1];
     swapA[1] = swapA[6];
     swapA[6] = swapB;
     swapB = swapA[2];
     swapA[2] = swapA[5];
     swapA[5] =swapB;
     swapB = swapA[3];
     swapA[3] = swapA[4];
     swapA[4] = swapB;

     ptrTemp = (double *)swapA;
     ptrValue = ptrTemp;

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

#define swapByte(a, b, t) t=*(a); *(a)=*(b); *(b)=t

void byteSwapDouble(double *ptrValue)
    char temp;
    swapByte(ptrValue, ptrValue+7, temp);
    swapByte(ptrValue+1, ptrValue+6, temp);
    swapByte(ptrValue+2, ptrValue+5, temp);
    swapByte(ptrValue+3, ptrValue+4, temp);
Sorry, I made mistake

#define swapByte(a, b, t) t=*(a); *(a)=*(b); *(b)=t

void byteSwapDouble(double *ptrValue)
   char temp; char *ptr = ptrvalue;

   swapByte(ptr, ptr+7, temp);
   swapByte(ptr+1, ptr+6, temp);
   swapByte(ptr+2, ptr+5, temp);
   swapByte(ptr+3, ptr+4, temp);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShinjjiAuthor Commented:
Good stuff.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.