[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: I forgot... Re: Big-endian/little-endian




On Tue, 18 Aug 1998, Ossama Othman wrote:

> Hi again,
> 
> > 	// "number" is the size of the array being byte swapped
> > 	swap4(void *dest, void *src, int number) {
> 
> I forgot to mention that the code excerpt I posted was meant for swapping
> variables that are 4 bytes in size, e.g. floats and such.

It is possible to write a similar macro with automatic size detection,
using the sizeof operator:

#define swapbt(x) swapbytes((char *) &x,sizeof(x))

void swapbytes(char * p, int count)
{
 int i=0;
 int j=count-1;
 char tmp;
 while (i<j)
   {
   /* Swapping "in place" */
   tmp=p[i];
   p[i]=p[j];
   p[j]=tmp;
   i++;
   j--;
   }
}

However use this code in C++ very carefully, results of swapping the
object (particularly of class with virtual functions) may be very
interesting :-).
Please consider the code above to be public domain

					Wojtek Zabolotny
					wzab@ipebio15.ipe.pw.edu.pl



Reply to: