Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

char to float conversion

Posted on 2006-11-11
3
2,567 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 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));
0
 

Author Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

829 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