Sprintf dont work!

Hello, I am using e2 studio compiler and RL78 G13 MCU.

 

I have a program where I get a floating value. But at the moment of wanting to observe it via serial and use the expression

sprintf (temp_Buff, "% f", Spare_Power);

The program freezes and there is no way out of there.

Translated with Google translator hahaha

  • Hi EduardoBorgiaE,

    I did try to reproduce the raised issue but, as far as I've seen, sprintf() implementation does work as specified under the latest RL78's CC-RL compiler.

    Yet so, I'd recommend for you to consider using snprintf() instead for such operations, as it helps to prevent buffer overflows.

    I've also noticed a <blank_space> between % and f within your formatting string. It is actually %f (altogether).

    Here is the minimal code snippet which I used when trying to reproduce the issue alongside its resulting buffer.

  • In reply to Felipe T :

    I am using kpit gnu rl78-elf toolchain.

    I have tried again but the system dies. I solved it using a function ftoa to do the conversion. Thanks for helping brother.
  • In reply to Romeo Reyes Badillo:

    Hi Romeo,

    Thanks for this clarification. I could not reproduce the issue in the latest GNURL78 toolchain as well (v4.9.2.201703).

    The sprintf() function from the optimized library does work. The sprintf() from the newlib does work.

    I am unable to reproduce this issue at this end. Feel free to share the minimal code snippet which reproduces the issue.

    For issues specific to the GCC libraries function implementations for Renesas MCUs I'd also recommend this forum

    https://gcc-renesas.com/forum/

    from the maintainers of the Renesas GCC toolchains. 

    Good luck.

  • In reply to Felipe T :

    unsigned char str[20];
    unsigned char *ftos(float f,int precision)
    {
    memset(str,0,sizeof(str));
    float ff;
    ff = f;
    int a,b,c,k,l=0,m,i=0;
    // check for negetive float
    if(f<0.0)
    {
    str[i++]='-';
    f*=-1;
    }
    a=f; // extracting whole number
    f-=a; // extracting decimal part
    k = precision;
    // number of digits in whole number
    while(k>0)
    {
    l = pow(10,k);
    m = a/l;
    if(m>0)
    {
    break;
    }
    k--;
    }
    // number of digits in whole number are k+1

    /*
    extracting most significant digit i.e. right most digit , and concatenating to string
    obtained as quotient by dividing number by 10^k where k = (number of digit -1)
    */
    for(l=k+1;l>0;l--)
    {
    b = pow(10,l-1);
    c = a/b;
    str[i++]=c+48;
    a%=b;
    }
    str[i++] = '.';
    /* extracting decimal digits till precision */
    for(l=0;l<precision;l++)
    {
    f*=10.0;
    b = f;
    str[i++]=b+48;
    f-=b;
    }
    str[i]='\0';
    return str;
    }

    //


    /////// I add that it has certain limitations in terms of some values but it suits my need! Greetings.