Sinisterly
Converting Float type data to String - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C)
+--- Thread: Converting Float type data to String (/Thread-Converting-Float-type-data-to-String)



Converting Float type data to String - alok9shm - 06-05-2014

Can someone help me convert a float type input data into string?
I don't want to use any library function.
For example:

The input goes like this:
scanf("%f",&data);

And the output should be printed as:
printf("%s",converted_data);

[I tried but messed up every time. My last attempt was to fputs the float data into a text file and then fgetc it to an array and then printf it using %s. Failed miserably. :troll:]

Thanks.


RE: Converting Float type data to String - dropzon3 - 06-05-2014

If you're okay with using printf

Code:
printf("%f", data)

Will print the float, you can also specify formatters like width and precision(digits after decimal) ex:

Code:
printf("%010.2f",data)

Will print the float using 10characters and using 0 as padding. The precision is 2 digits.

So from understanding printf, you can just use sprintf to print it into a string.

Code:
sprintf(str, "%010.2f",data)

Where str is the destination pointer, then you can print that string as desiried:

Code:
printf("%s",str);



RE: Converting Float type data to String - alok9shm - 06-05-2014

(06-05-2014, 07:23 AM)dropzon3 Wrote:
Code:
sprintf(str, "%010.2f",data)

Thats a restriction. I can't use sprintf.


RE: Converting Float type data to String - dropzon3 - 06-05-2014

(06-05-2014, 10:31 AM)alok9shm Wrote:
(06-05-2014, 07:23 AM)dropzon3 Wrote:
Code:
sprintf(str, "%010.2f",data)

Thats a restriction. I can't use sprintf.

What can you use?

Quote:My last attempt was to fputs the float data into a text file and then fgetc it to an array and then printf it using %s. Failed miserably.

fgetc and fputs (along with getc and puts) are both part of libc as are the scanf and printf family of functions are you saying to not use any libc functions?


RE: Converting Float type data to String - alok9shm - 06-05-2014

(06-05-2014, 10:40 AM)dropzon3 Wrote: What can you use?



fgetc and fputs (along with getc and puts) are both part of libc as are the scanf and printf family of functions are you saying to not use any libc functions?

I had to answer this question last day during my exam. Couldn't do it. Lost 7 marks. Its was clearly mentioned in that stupid question, not to use sprintf. Thats what I meant to say.


RE: Converting Float type data to String - dropzon3 - 06-05-2014

Well you could do something like the following but it relies on the math library. It basically just calculates each digit and adds it to the string.

Code:
char* ftoa(float num) { char *fstr, *strStart; float weight; int m,digit; m = floor(log10(num)); fstr = malloc(32*sizeof(char)); memcpy(fstr,"0",32); strStart = fstr; while (num > 0 + .0001f) { weight = pow(10.0f, m); digit = floor(num / weight); num -= (digit*weight); *(fstr++) = '0' + digit; if (m == 0) { *(fstr++) = '.'; } m--; } *(fstr) = '\0'; return strStart; }

Check out http://stackoverflow.com/questions/2302969/how-to-implement-char-ftoafloat-num-without-sprintf-library-function-i for several altnerative implementations(I based this one off the first answer)


RE: Converting Float type data to String - ArkPhaze - 06-12-2014

I see magic numbers here:
Code:
fstr = malloc( 32 * sizeof( char ) ); memcpy( fstr, "0", 32 );

Which should be: (including <limits.h>)
Code:
fstr = malloc( sizeof(num) * CHAR_BIT * sizeof( char ) ); memcpy( fstr, "0", sizeof(num) );


And instead of using memcpy after the fact, I would've used calloc():
Code:
fstr = calloc(sizeof(num) * CHAR_BIT , 1);

sizeof(char) is guaranteed to be 1 anyways, so using the sizeof() operator is redundant.

The other stupid thing about that code is that they only allocate 32 bytes... (What about -> float f = 1.9876E+38; ??) Assuming that the digits, including the decimal within that floating point number, stay within the allocated space, hence potential for buffer overflow. However, this won't work:
Code:
float f = 128038.329; char *s = ftoa( f ); printf( "%f\n", f ); printf( "%s\n", s );

Precision in the conversion to a c-style string is lost.

In that code though, I have no clue why the person used memcpy() to zero out the memory block, then proceed to add a null terminator anyways at the end:
Code:
*(fstr) = '\0';

Kind of redundant wouldn't you think? Although, this would make sense and be only useful if the allocated space was overflowed, in which case, the end null terminator is lost anyways, and buffer overflows are not good. If he's going to manually write the null terminator, why did he even bother with using memcpy() to write a bunch of '\0's to that allocated memory block?

IMHO, that is no code to learn from.

To do this properly, I think the best thing to do would be to parse the bytes manually and look at each segment of bits within that numeric value based on the IEEE 754 standard. Parse out the sign, exponent, and mantissa manually, and use that to determine the string.