Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

sprintf format issue

Hi,

I am doing a simple thing with string formatting:

sprintf(tmp_str,"%d.%d.%3d", version.major, version.minor, version.patch);

The version structure has values { 0, 5, 1 } and char tmp_str[20]. I want to achieve string like "0.5.001". Instead of getting this format, I am getting "0.5.d". 

For a curiosity I tried to change the format to "%3d.%d.%d" and I got "d.0.5". Format "%d.%d.%d" works as expected - 0.5.1.

I am using SDK 17.0.2 and SES 5.60.

Can somebody advise me how to brush sprintf to work as expected? I am not asking for workarounds how to get the expected string format rather to get sprintf working.

Thanx.

Best regards,

peter

Parents Reply
  • Hi Peter

    The following code seems to work fine for me:

    int a = 0;
    int b = 5;
    int c = 1;
    uint8_t testbuf[65];
    sprintf(testbuf, "Test %d.%d.%.3d", a, b, c);
    printf("%s\r\n", testbuf);

    The printed output is this:

    Test 0.5.001

    Could you try to run the same code?

    If it still doesn't work, can you let me know which of the projects in the SDK you are using?
    I based my test on the ble_app_uart example. 

    Best regards
    Torbjørn

Children
No Data
Related