?
Solved

char to float conversion

Posted on 2006-11-11
3
Medium Priority
?
2,578 Views
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
   }
0
Comment
Question by:prain
3 Comments
 
LVL 86

Expert Comment

by:jkr
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 );
}


0
 
LVL 9

Accepted Solution

by:
jhshukla earned 300 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));
0
 

Author Comment

by:prain
ID: 17923542
Thanks. Works great.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

809 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