This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to print a uint64_t variable?

Although this is a similar question to Case ID: 116570 "sprintf with a uint64_t variable?"
I am unable to use the answer:
I cannot find LDFLAGS.
I can find... SDK/external/segger_rtt/SEGGER_RTT_Syscalls_SES.c:#include "__libc.h"

TEST CODE:

uint64_t timestamp = 1422028920000;
char buffer[32];
snprintf(buffer, sizeof(buffer), "%" PRIu64 "", timestamp);
NRF_LOG_INFO("TEST PRINT ? 1422028920000 == %s", buffer);

OUTPUT:

app: TEST PRINT ? 1422028920000 == u

I am using nRF5_SDK_16.0.0
Code derived from examples/ble_peripheral/ble_app_hrs/pca10056/s140/

#include
#include
#include "nordic_common.h"
#include "nrf.h"
#include "nrf_sdm.h"
#include "app_error.h"
#include "ble.h"
#include "ble_err.h"
#include "ble_hci.h"
#include "ble_srv_common.h"
#include "ble_advdata.h"
#include "ble_advertising.h"
#include "ble_bas.h"
#include "ble_hrs.h"
#include "ble_dis.h"
#include "ble_conn_params.h"
#include "sensorsim.h"
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "nrf_sdh_soc.h"
#include "app_timer.h"
#include "bsp_btn_ble.h"
#include "peer_manager.h"
#include "peer_manager_handler.h"
#include "fds.h"
#include "nrf_ble_gatt.h"
#include "nrf_ble_lesc.h"
#include "nrf_ble_qwr.h"
#include "ble_conn_state.h"
#include "nrf_pwr_mgmt.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#include "my_service.h"
#include
#include
#include "ble.h"
#include "ble_srv_common.h"
#include "my_control.h"
#include "sdk_common.h"
#include
#include "nrf_gpio.h"

Parents
  • which compiler ? for GCC you need to use newlib for that.  nano lib doesn't work.

  • Thanks,

    I am using whatever default  compiler is provided by  SEGGER Embedded Studio for ARM V4.30. What would that be?
    Perhaps you can suggest on introduction to using SEGGER and selecting suitable libraries? I cant see any reference to "nano lib".
    Could I download "newlib" from somewhere and add a it to the project by editing my_project.emProject  ?

    In the meantime I re-invented a rough wheel, which works using printf:

    /** String representation of uint64_t
    * char[] output buffer
    * length of output buffer
    * uint64_t input
    * (radix == 10) | (radix == 16)
    */
    void toString_uint64_t(char * output, const int length, uint64_t input, const int radix ) {
    if ( (radix != 10) & (radix != 16) ) {
    output[0] = 'R';
    output[1] = 'A';
    output[2] = 'D';
    output[3] = 'I';
    output[4] = 'X';
    output[5] = '?';
    output[6] = 0; // null terminator
    return;
    }
    const char digits[] = {'0', '1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
    int head = length;
    for(int i = 0; ((i < length) & (input > 0)); i++ ) {
    head--;
    char digit = digits[ input % radix ];
    output[head] = digit;
    input /= radix;
    }
    // shift output and add string terminator:
    int count = length - head;
    for( int i = 0; (i < count); i++ ) {
    output[i] = output[ head + i ];
    }
    for( int i = count; (i < length); i++ ) {
    output[i] = 0; // null terminator
    }

    }

Reply
  • Thanks,

    I am using whatever default  compiler is provided by  SEGGER Embedded Studio for ARM V4.30. What would that be?
    Perhaps you can suggest on introduction to using SEGGER and selecting suitable libraries? I cant see any reference to "nano lib".
    Could I download "newlib" from somewhere and add a it to the project by editing my_project.emProject  ?

    In the meantime I re-invented a rough wheel, which works using printf:

    /** String representation of uint64_t
    * char[] output buffer
    * length of output buffer
    * uint64_t input
    * (radix == 10) | (radix == 16)
    */
    void toString_uint64_t(char * output, const int length, uint64_t input, const int radix ) {
    if ( (radix != 10) & (radix != 16) ) {
    output[0] = 'R';
    output[1] = 'A';
    output[2] = 'D';
    output[3] = 'I';
    output[4] = 'X';
    output[5] = '?';
    output[6] = 0; // null terminator
    return;
    }
    const char digits[] = {'0', '1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
    int head = length;
    for(int i = 0; ((i < length) & (input > 0)); i++ ) {
    head--;
    char digit = digits[ input % radix ];
    output[head] = digit;
    input /= radix;
    }
    // shift output and add string terminator:
    int count = length - head;
    for( int i = 0; (i < count); i++ ) {
    output[i] = output[ head + i ];
    }
    for( int i = count; (i < length); i++ ) {
    output[i] = 0; // null terminator
    }

    }

Children
Related