Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1461
  • Last Modified:

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;
}
----------------------------------------

0
Shinjji
Asked:
Shinjji
  • 2
1 Solution
 
KocilCommented:
#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);
}
0
 
KocilCommented:
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);
}
0
 
ShinjjiAuthor Commented:
Good stuff.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now