Search code examples
cstm32stm32cubeide

How do I display a 64bit unsigned integer with sprintf?


I'm displaying a 32bit timer value on my putty-console. The timer includes the time in microseconds since startup of my stm32wb55. With following code, it works exaclty like I want:

uint32_t time_micro32

sprintf((char*)buf,
          "Time: %lu \r\n",
         (time_micro32));

But now, I want to display the time in 64bit resolution with uint64_t time_micro64. I tried many, but nothing works. Can anybody help me, please? I´m programming in STM32CubeIDE


Solution

  • I´m programming in STM32CubeIDE

    So you are using most probably using newlib in it's "nano" version - a smaller version of newlib, which does not handle printing 64-bit variables.

    Link with full newlib version (or use other C standard library) and the code will work fine. Search your gui - there should be an option like "use -nano version of the standard library" or "pass -specs=nano.specs to the compiler". Uncheck that option. Make sure that -lnano nor -specs=nano.specs or similar options are not passed to the compiler. After that, you can use %llu or "%"PRIu64.

    Prefer to use snprintf instead of sprintf.

    Are there any other options?

    Roll your own implementation that would convert 64-bit variable into a string - writing such function is very trivial. Then use this function and print the resulting string. I have such function.