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
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)
> 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.)
> 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?
>
>
> >