Big endian to Little endian 
Author Message
 Big endian to Little endian

Hi,

I have to convert 32-bit integers and 64-bit doubles of big endian type
to little endian type.
I have already an idea for integers, but I am not sure if this is really
a fast way.  I do not want
to use any special library, because the program should be portable.

Thanks



Sat, 05 Jul 2003 15:54:42 GMT  
 Big endian to Little endian

Quote:

>I have to convert 32-bit integers and 64-bit doubles of big endian type
>to little endian type.
>I have already an idea for integers, but I am not sure if this is really
>a fast way.  I do not want
>to use any special library, because the program should be portable.

Your specifications are self-contradictory, like asking for a color
image that is only in shades of grey.  If Machine X has 64-bit
doubles using base 16, and Machine Y has 80-bit doubles using base
2, any conversion code you produce is not going to work well on
machine Z that has 64-bit base-2 doubles.

Your best bet in general is to avoid depending on *any* native
machine endian-ness or encoding.  If you print the values out in
human-readable notation on Machine X, and read them back on Machine
Y, you will, in general, have an easy way to get a reasonably
accurate conversion between the two.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc




Sat, 05 Jul 2003 20:00:47 GMT  
 Big endian to Little endian

Quote:

> Hi,

> I have to convert 32-bit integers and 64-bit doubles of big endian type
> to little endian type.
> I have already an idea for integers, but I am not sure if this is really
> a fast way.  I do not want
> to use any special library, because the program should be portable.

> Thanks

Hi,

presumably you want to do this for passing data between two computers on
a network. In which case, if you have a network functions extension for
you C library, you might find the macros htonl and ntohl useful. These
macros will (efficiently) swap the bytes for 32 bit values (or not as
necessary). If you use htonl on any unsigned long to be sent on the
network, and ntohl on any received unsigned long the right thing will
happen.

I think swapping doubles is much harder, since there is no guarantee
floating point numbers will be handled in a similar way between the
processors (I mean they might have a different layout for floating
point, such as different sizes for mantissa and exponent).

I've not had to pass floating point numbers across a network, so others
may have a better answer. The most portable way I can think of is to
pass the string representation of the number, which is easy to generate
and requires no consideration of endianness.

Alternatively split the floating point number into unsigned long
components which represent the mantissa and exponent (probably two
unsigned longs for the mantissa to keep the accuracy). Then you can use
the above macros for transmission and receipt. On receipt you would have
to reform the floating point number from the components. This is going
to be a lot of work, so may not be what you want.

        Des Walker
        Alenia-Marconi Systems



Sat, 05 Jul 2003 20:23:00 GMT  
 Big endian to Little endian

Quote:

> I have to convert 32-bit integers and 64-bit doubles of big endian
> type to little endian type.

> I have already an idea for integers, but I am not sure if this is
> really a fast way.  I do not want to use any special library, because
> the program should be portable.

I think you are looking at this from the wrong perspective - How
did those items get into your machine (or get out) in the first
place?  This defines one end of the problem.  At your end, you
have values, which you form (or unpack) by combinations of
shifting and masking.  That way your code is portable and
independent of internal endianess.

--

http://www.qwikpages.com/backstreets/cbfalconer
   (Remove "NOSPAM." from reply address. my-deja works unmodified)



Sat, 05 Jul 2003 21:06:21 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Big Endian->Little Endian for floats

2. Big Endian vs Little Endian and other questions

3. big-endian to little-endian?

4. converting between big endian and little endian binary files

5. big-endian VS little-endian

6. Big endian, little endian question.

7. Read double little endian on big endian machine

8. Big Endian/Little Endian

9. big endian on SUN to little endian on Intel conversion

10. big-endian vs. little-endian

11. Big Endian/Little Endian

12. little-endian and big-endian in bit-order

 

 
Powered by phpBB® Forum Software