Solved

char to float conversion

Posted on 2006-11-11
3
2,559 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
Comment Utility
'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 75 total points
Comment Utility
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
Comment Utility
Thanks. Works great.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now