Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Byte order question

1 view
Skip to first unread message

thak...@egr.msu.edu

unread,
Nov 24, 1997, 3:00:00 AM11/24/97
to

Hi!
How do I take care of byte ordering when I am sending a float or a
double from one machine with little endian architecture to another machine
with big endian architecture.

I can do this with integers using htonl,htons,ntohl,ntohs funtions. How
do I handle float datatype?

Thanx

Vishal

-------------------==== Posted via Deja News ====-----------------------
http://www.dejanews.com/ Search, Read, Post to Usenet

Marc Lampo

unread,
Nov 25, 1997, 3:00:00 AM11/25/97
to

thak...@egr.msu.edu wrote:
>
> Hi!
> How do I take care of byte ordering when I am sending a float or a
> double from one machine with little endian architecture to another machine
> with big endian architecture.
>
> I can do this with integers using htonl,htons,ntohl,ntohs funtions. How
> do I handle float datatype?
Hello,

considered XDR ?

Create yourself an XDR-handle of type "mem" :
void xdrmem_create(xdrs, addr, size, op)
XDR *xdrs;
char *addr;
u_int size;
enum xdr_op op;

XDR handle;
char array[ARRAY_SIZE];
xdrmem_create (&handle, array, ARRAY_SIZE, XDR_ENCODE);
/* always check return values ! */

Then you convert the float (float is known data type) :
xdr_float (&handle, &float);
/* take care : call by reference */

The converted float is now available at address "array" over
"xdr_getpos (&handle)" bytes. Now you could use it in a write/sendto.

Reset the pointer in the handle :
xdr_setpos (&handle, 0);

(a subsequent invocation of xdr_float - or another encoding routine -
will then restart at the indicated position)

At the receiving side, create a handle for DECODING :
XDR handle;
char array[ARRAY_SIZE];
xdrmem_create (&handle, array, ARRAY_SIZE, XDR_DeCODE);

Now put bytes to be decoded in the array (read/recvfrom) and decode :
float f;
xdr_float (&handle, &f);
/* call by reference ! */
Now "f" holds the decoded float value in a format understandable for
the local machine.


For info on the xdr :
man xdr_simple (for simple "filters")
man xdr_complex (if you've array's of simple data-types)
man xdr_admin (for taking care af the XDR handle, positioning eg)
man xdr_create (for creating and destroying handles)


I think a not unimportant message is :
one can use XDR without being obliged to use RPC.
Secondly : be aware that while encoding, as opposed to converting to
ASCII,
one loses the possibility of being capable of "telnet host
your_application"
and testing like that.

Hope this helps, success,

Marc Lampo
EUnet Belgium NV

(Guest teacher of "Client/Server Programming" at
AT Computing, Nijmegen, The Netherlands)

Bjorn Reese

unread,
Nov 26, 1997, 3:00:00 AM11/26/97
to

thak...@egr.msu.edu wrote:

> How do I take care of byte ordering when I am sending a float or a
> double from one machine with little endian architecture to another machine
> with big endian architecture.

I believe converting it to a text string and back will cause you least
trouble (if you can accept possible truncation errors -- if you can't
try looking for the XDR format.)

Sven Norén

unread,
Nov 28, 1997, 3:00:00 AM11/28/97
to Marc Lampo

I think you should use the procedure ntohs.
/Sven
Marc Lampo wrote:

> thak...@egr.msu.edu wrote:
> >
> > Hi!


> > How do I take care of byte ordering when I am sending a float or a
> > double from one machine with little endian architecture to another
> machine
> > with big endian architecture.
> >

> > I can do this with integers using htonl,htons,ntohl,ntohs funtions.
> How
> > do I handle float datatype?
>
>
> >

0 new messages