Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Sending Float values... (http://www.velocityreviews.com/forums/t558433-sending-float-values.html)

swetha 12-09-2007 05:41 AM

Sending Float values...
 
Hi,
I worte a RPC program for client and server, After computing
(which i already did)..i get a float value.Now i want this value to be
sent to the client .I know how to do with integer (By passing the
addreess using the pointers,but when i am doing the same here iam
getting errors.Can any 1 tell how to do that please.....
for example say i have the value in result variable ;which is
float....now how should i declare and send this to the client side....

Malcolm McLean 12-09-2007 08:06 AM

Re: Sending Float values...
 

"swetha" <laptop545@gmail.com> wrote in message
> I worte a RPC program for client and server, After computing
> (which i already did)..i get a float value.Now i want this value to be
> sent to the client .I know how to do with integer (By passing the
> addreess using the pointers,but when i am doing the same here iam
> getting errors.Can any 1 tell how to do that please.....
> for example say i have the value in result variable ;which is
> float....now how should i declare and send this to the client side....
>

You need to convert the floats to a common binary format.

If you read the maths section in my book Basic Algorithms (chapter avialable
free, on my website) you will see floating point representation explained.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


James Fang 12-10-2007 04:15 AM

Re: Sending Float values...
 
On 12月9日, 下午1时41分, swetha <laptop...@gmail.com> wrote:
> Hi,
> I worte a RPC program for client and server, After computing
> (which i already did)..i get a float value.Now i want this value to be
> sent to the client .I know how to do with integer (By passing the
> addreess using the pointers,but when i am doing the same here iam
> getting errors.Can any 1 tell how to do that please.....
> for example say i have the value in result variable ;which is
> float....now how should i declare and send this to the client side....


try to use asc string instead of the float representation in memory:

#define MAX_FLOAT_LEN 32

int main() {
float a = 1.1;
char floatArray[MAX_FLOAT_LEN];
memset(floatArray,0,sizeof(floatArray));
snprintf(floatArray,sizeof(floatArray),"%f",a);
printf("%s\n",floatArray);
}

you can call send(fd, floatArray, strlen(floatArray),0); to send the
char array to the remote peer.
If you are using TCP as the transport layer protocol, it's better to
add "\r\n" in the end of the float string, so that you can distinguish
the float string from the remote
host:snprintf(floatArray,sizeof(floatArray),"%fr\n ",a);

James Fang 12-10-2007 04:16 AM

Re: Sending Float values...
 
On 12月10日, 下午12时15分, James Fang <fangshang...@gmail.com> wrote:
> On 12月9日, 下午1时41分, swetha <laptop...@gmail.com> wrote:
>
> > Hi,
> > I worte a RPC program for client and server, After computing
> > (which i already did)..i get a float value.Now i want this value to be
> > sent to the client .I know how to do with integer (By passing the
> > addreess using the pointers,but when i am doing the same here iam
> > getting errors.Can any 1 tell how to do that please.....
> > for example say i have the value in result variable ;which is
> > float....now how should i declare and send this to the client side....

>
> try to use asc string instead of the float representation in memory:
>
> #define MAX_FLOAT_LEN 32
>
> int main() {
> float a = 1.1;
> char floatArray[MAX_FLOAT_LEN];
> memset(floatArray,0,sizeof(floatArray));
> snprintf(floatArray,sizeof(floatArray),"%f",a);
> printf("%s\n",floatArray);
>
> }
>
> you can call send(fd, floatArray, strlen(floatArray),0); to send the
> char array to the remote peer.
> If you are using TCP as the transport layer protocol, it's better to
> add "\r\n" in the end of the float string, so that you can distinguish
> the float string from the remote


an amendment to my above post:

> host:snprintf(floatArray,sizeof(floatArray),"%f\r\ n",a);




All times are GMT. The time now is 01:48 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.