2

I need to convert a float value to a string in my avr project. Therefore I tried it with the following code:

char buf[10];
float emg1 = 33.42;
sprintf(buf, "%f", emg1);
uart_puts(buf);

But the output is only a "?". I tried to change the char format from %f to %g but I only got "?".

Is there another way to simply convert float to string, or can somebody tell me where the mistake is?

6
  • Maybe your printf implementation doesn't support %f. Did you double check with e.g. sprintf(buf, "%d", 123) just to be sure that the problem is not elsewhere? Commented Apr 28, 2022 at 12:33
  • I don't see the issue with string conversion. Sometimes due to sync error bits are wrongly interpreted. Commented Apr 28, 2022 at 12:34
  • with %d and a int it works Commented Apr 28, 2022 at 12:34
  • 2
    -Wl,-u,vfprintf -lprintf_flt -lm has to be enabled for float, as per avr doc. Commented Apr 28, 2022 at 12:43
  • 1
    Tip: sprintf(buf, "%f", emg1); is not certainly enough info to convert the string back to the same float. Better as sprintf(buf, "%.8e", emg1); or sprintf(buf, "%.9g", emg1); with a larger buffer (about 17+). Commented Apr 28, 2022 at 12:46

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.