float var, htonl(var), a clean way to do this?

bridged with qdn.public.qnxrtp.applications
Post Reply
Miguel Simon

float var, htonl(var), a clean way to do this?

Post by Miguel Simon » Tue Apr 23, 2002 5:06 pm

Hi...

Is there a clean equivalent to htonl and family that works with float
and double for data transfer between LE and BE machines??

Thanks...

Regards...

Miguel

--
---
my opinions are mine, only mine, solely mine, and they are not related
in any possible way to the institution(s) in which I study and work.
---
Miguel Simon
Research Engineer
School of Aerospace and Mechanical Engineering
University of Oklahoma
http://www.amerobotics.ou.edu/
http://www.saic.com

Miguel Simon

Re: float var, htonl(var), a clean way to do this?

Post by Miguel Simon » Wed Apr 24, 2002 4:03 pm

Hi...

a pointer to an answer follows. Has any body have any experience,
pointers, etc with regard to this?? Thanks.

Miguel.


from:

http://groups.google.com/groups?q=xdrme ... net&rnum=6

=========================================
thakkarv@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)

Thanx

Vishal
===============================

Miguel Simon wrote:
Hi...

Is there a clean equivalent to htonl and family that works with float
and double for data transfer between LE and BE machines??

Thanks...

Regards...

Miguel

--
---
my opinions are mine, only mine, solely mine, and they are not related
in any possible way to the institution(s) in which I study and work.
---
Miguel Simon
Research Engineer
School of Aerospace and Mechanical Engineering
University of Oklahoma
http://www.amerobotics.ou.edu/
http://www.saic.com

Post Reply

Return to “qdn.public.qnxrtp.applications”