char to float conversion

Posted on 2006-11-11
Last Modified: 2010-09-24
Here is my problem.

I am transferring data from a client to a server via a socket.
On the client side data is defined as float[]. Before sending it to the server, I convert the
float[] into a char* using memcpy.

I receive the data on the server side as a buffer of characters (each float value represented by 4 character bytes). Then I am trying to convert 4-bytes (characters) together to form a float. Does not work.

I do not want to publish all code here. But here is what I am doing in very short way....

1. Client side....
    char flt[] = {45.6f, 34.f, 67.5f};
    char *char_flt = new char[sizeof(flt) * 4];   //Size mult by 4 beacuse each float is 4 bytes
    memcpy(char_flt, flt, sizeof(flt) * 4);

2. Server side .... Buffer is received via the socket into a char*
    this is sent to a function to convert back to floats and process...

   void mem_copy(char *cpyDat,  unsigned int size)
    //HOW DO I COMBINE 4 characters together to FORM A float value
   Note : I tried using atof() taking first 4 bytes (chars), creating a string having null charcter at the end.
   But it did not make the 45.6 first value.
   //Here is what I did.
   char flt_char[5];
   for (int i = 0; i < 4; i++)
     flt_char[i] = cpyDat[i];
    flt_char[4] = '0';   //Make sure null character at the end
    cout << atof(flt_char);     //Did not make the first 45.6
Question by:prain
LVL 86

Expert Comment

ID: 17921816
'memcpy()' won't get you anwhere, since float is binary. Use 'gcvt()' and 'atof()':

/* _GCVT.C: This program converts -3.1415e5
 * to its string representation.

#include <stdlib.h>
#include <stdio.h>

void main( void )
   char buffer[50];
   double source = -3.1415e5;
   _gcvt( source, 7, buffer );
   printf( "source: %f  buffer: '%s'\n", source, buffer );
   _gcvt( source, 7, buffer );
   printf( "source: %e  buffer: '%s'\n", source, buffer );

/* ATOF.C: This program shows how numbers stored
 * as strings can be converted to numeric values
 * using the atof, atoi, and atol functions.

#include <stdlib.h>
#include <stdio.h>

void main( void )
   char *s; double x; int i; long l;

   s = "  -2309.12E-15";    /* Test of atof */
   x = atof( s );
   printf( "atof test: ASCII string: %s\tfloat:  %e\n", s, x );

   s = "7.8912654773d210";  /* Test of atof */
   x = atof( s );
   printf( "atof test: ASCII string: %s\tfloat:  %e\n", s, x );

   s = "  -9885 pigs";      /* Test of atoi */
   i = atoi( s );
   printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );

   s = "98854 dollars";     /* Test of atol */
   l = atol( s );
   printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );


Accepted Solution

jhshukla earned 75 total points
ID: 17923402
what the heck is this?
>> char flt[] = {45.6f, 34.f, 67.5f};

correct me if i am wrong but what i think you are attempting to do is transmit an array of floats.

// sender
float float_arr[ARR_SIZE] = {...};
char *buf = new char[ARR_SIZE*sizeof(float)];
memcpy(buf, float_arr, ARR_SIZE*sizeof(float));
// send buf here
// receiver
float float_arr[ARR_SIZE];
char *buf = new char[ARR_SIZE*sizeof(float)];
// read buf here.
memcpy(float_arr, buf, ARR_SIZE*sizeof(float));

Author Comment

ID: 17923542
Thanks. Works great.

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question