This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Convert int64_t to string

I'm using NCS 1.8.0 and nRF9160 DK, and I need to convert unix_time from int64_t to string, my problem is exactly what is reported in these cases:

devzone.nordicsemi.com/.../snprintf-int64_t-not-working
https://devzone.nordicsemi.com/f/nordic-q-a/83967/long-long-int-value-sprintf

My conversion attempts can be seen below, but in all of them I don't get the number in value_buf_time, but I get lu.

snprintf(value_buf_time, sizeof(value_buf_time), "%"PRIu64, unix_time_ms);
printk("Publicando o time: %s ",value_buf_time);
or
snprintf(value_buf_timesizeof(value_buf_time), "%ld"unix_time_ms);
printk("Publicando o time: %s ",value_buf_time);
or
snprintf(value_buf_timesizeof(value_buf_time), "%lld"unix_time_ms);
printk("Publicando o time: %s ",value_buf_time);
The output of all printk is:
 
Publicando o time: lu
 
In other cases the solution was to insert CONFIG_NEWLIB_LIBC_NANO=n. But I can't insert this because when I have LIBC_NANO=n I end up getting a HARD_FAULT in the lte_lc_init_and_connect() function call. Is there another solution to make this conversion work?
Thank you.
Parents
  • Thanks a lot for your feedback, Gabriel!

    Thabet said:
    To make it easier for you to reproduce the error, I've replicated this behavior in the mqtt_simple sample. To get unix_time I'm using the date_time library, and I need to add in prj.conf:

    Could you share the lines you have added to main.c of the mqtt_simple sample as well?

    Regards,

    Markus

Reply
  • Thanks a lot for your feedback, Gabriel!

    Thabet said:
    To make it easier for you to reproduce the error, I've replicated this behavior in the mqtt_simple sample. To get unix_time I'm using the date_time library, and I need to add in prj.conf:

    Could you share the lines you have added to main.c of the mqtt_simple sample as well?

    Regards,

    Markus

Children
  • I ended up not adding anything to the main of the mqtt_simple sample, I was going to do that but I noticed that just adding the 3 settings to prj.conf the sample no longer works, the same way it did in my application.

    But in my application I add the following lines to main to get the unix time:

    #include <date_time.h>

    int64_t unix_time_ms;

    date_time_now(&unix_time_ms);

    printk("Measurement Instant %d: %lld \n",i, unix_time_ms);

    My output is:

    Measurement Instant 0: 1646661619367

    This works, I get the correct unix_time! But I can't turn this value into a string, when I try:

    snprintf(InstMed[i], sizeof(InstMed[i]), "%lld", unix_time_ms);

    printk("Instant of Measurement %d: %s \n",i, InstMed[i]);

    My output is:

    Measurement Instant 0: ld

    So to solve this, following the recommendations found in Devzone I must add CONFIG_NEWLIB_LIBC_NANO=n, so that it is possible to transform int64_t into a string, and when I add this the error occurs.

Related