Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

char to float conversion

Posted on 2006-11-11
3
Medium Priority
?
2,576 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
[X]
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
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

618 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