8 years, 6 months ago.

Converting float value to char *array

Hello, I have a pointer array. I am trying to convert float value into char so I can store it in the array. How it is possible? I am using a way but it is not working. The compiler is building it but it is not working in real system.

This is my array -> char *zone_value[]={"0.00","0.00","0.00","0.00"," "};

float value is -> float value=0.25;

sprintf(zone_value[zone],"%f",value );

Any suggestions?

3 Answers

8 years, 6 months ago.

I'm using this for converting a single float (value) to a char array:

uint8_t *floatArray = (uint8_t *) &value; /* ftoa */

sstaub

8 years, 6 months ago.

Your original code could easily corrupt the storage space...

// zone_value is an array of pointers.
// zone_value[0] points to a string with only 5 storage locations: '0', '.', '0', '0', '\0'
char *zone_value[]={"1.23","4.56","7.89","0.00"," "};
// In memory as \x31, \x2E, \x32, \x33, \x00, \x34, \x2E, \x35, \x36, ...

// The following sprintf could easily overrun the storage as the value 
// might expand to a string greater than the original storage.
sprintf(zone_value[zone], "%f", value);  // if value was 1.234 it would overrun the storage
// In memory as \x31, \x2E, \x32, \x33, \x00, \x34, \x2E, \x35, \x36, ...
// becomes      \x31, \x2E, \x32, \x33, \x34, \x00, \x2E, 

As an alternate, you might pre-allocate storage for the strings, and you will want to specify the formatter to control the expansion

#define MAX_LEN_FLT 10
#define NUM_ZONES 5
char * zone_value[NUM_ZONES][MAX_LEN_FLT];

// use the secure form of printf to avoid buffer overrun errors
// and reserve space for the sign, in case value could be < 0
int r = snprintf(zone_value[zone], MAX_LEN_FLT, "%+5.3f", value); // "-12.345"

// always a good idea to check the return code
if (r < 1 || r > MAX_LEN_FLT)
   printf("unable to convert value to a float\n");

Is there a good reference somewhere for the "safe" forms of these commands. I don't think the textbooks I have even mention them.

posted by Oliver Broad 25 Apr 2016

Hi Oliver,

That's a great question - which might deserve its own thread of discussion. I hope others can help answer. Some libraries append _s to the functions to indicate the secure version, but this is not the "standardized" form I guess. I think C++ string functions are probably more secure (e.g. string a; a += "hello";) but for some applications I avoid these as they can cause severe memory fragmentation. I tried searching for "secure c string functions" and there are a lot of hits.

Generally, anything that writes to a buffer, should also pass the size of the buffer, so the function can avoid overrun. "strcpy" gets replaced with "strncpy" for instance.

posted by David Smart 25 Apr 2016
8 years, 6 months ago.

Don't know what your application is, if you want to fill a dynamic float value array, I would do this, mainly as I use something similar. But this may be a homework question.

snip-tested

float zone_value[101];

    zone_value[0] = 0.00;
    zone_value[1] = 1.65;
    zone_value[2] = 0.00;
    zone_value[3] = 4.21;
    zone_value[4] = 3.68;
    // ........
    zone_value[99] = 6.37;
    
    for(int n = 0; n < 5; n++) {
        sprintf(buffer,"zone value %d: = %1.2f \n",n,zone_value[n]);         
    }
    sprintf(buffer,"zone value 99: = %1.2f \n",zone_value[99]);