[vtkusers] #Defines for Big or Little Endian

Mike Jackson mike.jackson at imts.us
Fri Sep 1 11:06:22 EDT 2006


On 9/1/06 10:56 AM, "Sean McBride" <sean at rogue-research.com> wrote:

> On 2006-08-31 16:35, Mike Jackson said:
> 
>> Is there a #define some where that I can use to test if I am running
>> on a big or little endian machine?
>> 
>> Beyond that, are there already methods some where that I have not
>> found that will take a value, say UIn32 and swap the bytes to whatever
>> the system is? Something like:
>> 
>> ByteSwap::swapLEToSystem( UInt32 );
> 
> I believe vtkByteSwap is the only class that does anything like this.
> You might want to search all the vtk code for "VTK_WORDS_BIGENDIAN" to
> learn a little more about how vtk does swapping.
> 
> Personally, I find Apple's APIs are much better named, but I believe
> vtkByteSwap::Swap4BE(), for example, swaps a 4 byte value from host to big.
> 
> --
> ____________________________________________________________
> Sean McBride, B. Eng                 sean at rogue-research.com
> Rogue Research                        www.rogue-research.com
> Mac Software Developer              Montréal, Québec, Canada
> 
> 
> _______________________________________________

No, actually vtkByteSwap::Swap4BE() does the swap period. It does not pay
attention to if it actually _needs_ to be done. It just does it.

In Apples code for swapping Little To System for instance there is a
compiler check to see if the code is compiled for a Little Endian machine.
If it _is_ compiled for a Little Endian machine then the method simply
returns the argument that was passed in. If on the other hand the code was
compiled on a BIG endian machine then the actual swap will take place. This
is a nice optimization that takes place on your behalf with out any work on
your part. 

 I am reading binary files that can be Big or Little Endian or either a Big
Or Little Endian machine. That is 4 combinations. If I use VTK's byte
swapping code I would have to write 4 versions of my code. One covering each
of the possibilities. By using API's such as those from Apple the compiler
can optimize 2 of those situations away for me so I only need to write 2
versions of my code.

Unless in my newbie C++ knowledge I have completely missed something...

Cheers
-- 
Mike Jackson





More information about the vtkusers mailing list