?
Solved

Byte swapping a double in C / C++

Posted on 2003-03-20
3
Medium Priority
?
1,408 Views
Last Modified: 2008-02-26
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
Comment
Question by:Shinjji
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Expert Comment

by:Kocil
ID: 8173349
#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
 
LVL 5

Accepted Solution

by:
Kocil earned 150 total points
ID: 8173360
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
 

Author Comment

by:Shinjji
ID: 8173874
Good stuff.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question