Sending Float values...

Discussion in 'C Programming' started by swetha, Dec 9, 2007.

  1. swetha

    swetha Guest

    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....
     
    swetha, Dec 9, 2007
    #1
    1. Advertising

  2. "swetha" <> 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
     
    Malcolm McLean, Dec 9, 2007
    #2
    1. Advertising

  3. swetha

    James Fang Guest

    On 12ÔÂ9ÈÕ, ÏÂÎç1ʱ41·Ö, swetha <> 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, Dec 10, 2007
    #3
  4. swetha

    James Fang Guest

    On 12ÔÂ10ÈÕ, ÏÂÎç12ʱ15·Ö, James Fang <> wrote:
    > On 12ÔÂ9ÈÕ, ÏÂÎç1ʱ41·Ö, swetha <> 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);
     
    James Fang, Dec 10, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    7
    Views:
    6,265
    Roedy Green
    May 10, 2004
  2. bd
    Replies:
    0
    Views:
    632
  3. Juergen
    Replies:
    3
    Views:
    293
    Duncan Grisby
    May 31, 2006
  4. Jukka K. Korpela
    Replies:
    1
    Views:
    1,130
    Jonathan N. Little
    Sep 12, 2006
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,553
    James Kanze
    Oct 8, 2009
Loading...

Share This Page