Big Endian->Little Endian for floats
Author Message
Big Endian->Little Endian for floats

Hi,

I have to transfer datas from a HPUX server to LINUX on another machine.
This means that I pass from a BIG-endian system to a Little one.
I have done the conversion for integers and chars.
But the problem is for floating-point;

I have tried many solutions but with no success :
for example :
int i;
unsigned char *buffer=(void *) &(value);
for (i=0;i<sizeof(value)/2;i++)
{
unsigned char tmp;
tmp=buffer[i];
buffer[i]=buffer[sizeof(value)-1-i];
buffer[sizeof(value)-1-i]=tmp;

Quote:
}

this doesn't work...

How can I do to convert from Big to Little Endian ?

Isabelle
FRANCE

Tue, 04 Jan 2005 19:48:52 GMT
Big Endian->Little Endian for floats

Quote:
> Hi,

> I have to transfer datas from a HPUX server to LINUX on another machine.
> This means that I pass from a BIG-endian system to a Little one.
> I have done the conversion for integers and chars.
> But the problem is for floating-point;

> I have tried many solutions but with no success :
> for example :
> int i;
> unsigned char *buffer=(void *) &(value);
> for (i=0;i<sizeof(value)/2;i++)
> {
> unsigned char tmp;
> tmp=buffer[i];
> buffer[i]=buffer[sizeof(value)-1-i];
> buffer[sizeof(value)-1-i]=tmp;
> }

> this doesn't work...

> How can I do to convert from Big to Little Endian ?

That won't work anyways because the format within the data is probably
different.  There are two off the top of my head solutions

1.  Write the output as two longs.  e.g.

long i, f;
double x;

i = (long)x;
f = (long)(fabs(x - (double)i) * pow(2, 32));

then send out (i, f) like you would normally [e.g. byte endian correct].

This won't handle cases where the magnitude of the mantissa is larger than
the largest possible long but for most values it will work.

2.  Output the value as an ascii string then just atof/scanf/etc it back.

Tom

Tue, 04 Jan 2005 20:12:36 GMT
Big Endian->Little Endian for floats
: I have to transfer datas from a HPUX server to LINUX on another machine.
: This means that I pass from a BIG-endian system to a Little one.
: I have done the conversion for integers and chars.
: But the problem is for floating-point;

This really isn't topical for comp.lang.c.  Different platforms
and implementations implement floats in different ways.   The best
(most portable in the Unix world) would be to use XDR. Try typing
comp.unix.programmer.  Your question is better handled by the posters
in that group.

Good Luck,

Paul

--
Paul D. Boyle

North Carolina State University
http://laue.chem.ncsu.edu/web/xray.welcome.html

Wed, 05 Jan 2005 01:40:23 GMT
Big Endian->Little Endian for floats

Quote:

> Hi,

> I have to transfer datas from a HPUX server to LINUX on another machine.
> This means that I pass from a BIG-endian system to a Little one.
> I have done the conversion for integers and chars.
> But the problem is for floating-point;

> I have tried many solutions but with no success :
> for example :
> int i;
> unsigned char *buffer=(void *) &(value);
> for (i=0;i<sizeof(value)/2;i++)
> {
> unsigned char tmp;
> tmp=buffer[i];
> buffer[i]=buffer[sizeof(value)-1-i];
> buffer[sizeof(value)-1-i]=tmp;
> }

> this doesn't work...

I do endian swaps of floating point values in a project of mine.

Assuming that the floats on the two platforms are compatible, I
do this:

void
endswap_float_array (float *f, int count)
{   int  temp, *iptr ;

iptr = (int*) f ;
while (count)
{   count -- ;
temp = iptr [count] ;
temp = ((temp >> 24) & 0xFF) | ((temp >> 8) & 0xFF00) |
((temp & 0xFF00) << 8) | ((temp & 0xFF) << 24) ;
iptr [count] = temp ;
}

Quote:
}

However, this function is only used after the host platform has been
tested to verufy that the above will work. If it doesn't work I have
code which breaks the float into sign, exponent and mantissa and
reassembles.

Erik
--
+-----------------------------------------------------------+

+-----------------------------------------------------------+
"Python is the most efficient language I've ever used. It's 10 times
better than any of the other tools I have used. It's free, it's
object-oriented, it adapts to everything, it runs on everything. There
is almost an indescribable 'quality without a name' attraction on my
part."   --Bruce Eckel, Author of Thinking in Java

Thu, 06 Jan 2005 06:36:56 GMT
Big Endian->Little Endian for floats

Quote:

> > Hi,

> > I have to transfer datas from a HPUX server to LINUX on another machine.
> > This means that I pass from a BIG-endian system to a Little one.
> > I have done the conversion for integers and chars.
> > But the problem is for floating-point;

> > I have tried many solutions but with no success :
> > for example :
> > int i;
> > unsigned char *buffer=(void *) &(value);
> > for (i=0;i<sizeof(value)/2;i++)
> > {
> > unsigned char tmp;
> > tmp=buffer[i];
> > buffer[i]=buffer[sizeof(value)-1-i];
> > buffer[sizeof(value)-1-i]=tmp;
> > }

> > this doesn't work...

> I do endian swaps of floating point values in a project of mine.

> Assuming that the floats on the two platforms are compatible, I
> do this:

> void
> endswap_float_array (float *f, int count)
> {   int  temp, *iptr ;

>     iptr = (int*) f ;
>     while (count)
>     {   count -- ;
>         temp = iptr [count] ;
>         temp = ((temp >> 24) & 0xFF) | ((temp >> 8) & 0xFF00) |
>                 ((temp & 0xFF00) << 8) | ((temp & 0xFF) << 24) ;
>         iptr [count] = temp ;
>         }
> }

> However, this function is only used after the host platform has been
> tested to verufy that the above will work. If it doesn't work I have
> code which breaks the float into sign, exponent and mantissa and
> reassembles.

The binary 'file' interface between or among disparate systems is simply
too complicated to deal with. Use text. fprintf() is your friend.
--

"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

Thu, 06 Jan 2005 23:18:48 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages