Skip to content
ph4r05 edited this page Jan 29, 2014 · 3 revisions

Welcome to the WSNProtectLayer wiki!

Formatting strings for printf:

Source: http://sourceforge.net/p/mspgcc/msp430-libc/ci/master/tree/include/inttypes.h

Brief explanation of taxonomy:

decimal printf format for int8_t:       #define PRId8    "d"
decimal printf format for int16_t:      #define PRId16   "d"
decimal printf format for int32_t:      #define PRId32   "ld"
decimal printf format for int64_t:      #define PRId64   "lld"

integer printf format for int8_t        #define PRIi8    "i"
integer printf format for int16_t       #define PRIi16   "i"
integer printf format for int32_t       #define PRIi32   "li"
integer printf format for int64_t       #define PRIi64   "lli"

decimal printf format for uint8_t       #define PRIu8    "u"
decimal printf format for uint16_t      #define PRIu16   "u"
decimal printf format for uint32_t      #define PRIu32   "lu"
decimal printf format for uint64_t      #define PRIu64   "llu"

hexadecimal printf format for uint8_t   #define PRIx8    "x"
hexadecimal printf format for uint16_t  #define PRIx16   "x"
hexadecimal printf format for uint32_t  #define PRIx32   "lx"
hexadecimal printf format for uint64_t  #define PRIx64   "llx"

Usage:

#include <inttypes.h>

  • Before: printf("%lu; ", some_uin32_t_variable);
  • After: printf("%" PRIu32 "; ", some_uin32_t_variable);

Why?

Using macros in formatting string is more intuitive for human, the bit length of the integer type is clear from the macro name. Header file inttypes.h is specific for the processor (MSP430 in our case for TelosB).

Floating numbers:

It is known that TinyOS has some issues with dealing with floating numbers in printf statements. Possible workaround:

void printfFloat(float toBePrinted) {
     uint32_t fi, f0, f1, f2;
     char c;
     float f = toBePrinted;

     if (f<0){
       c = '-'; f = -f;
     } else {
       c = ' ';
     }

     // integer portion.
     fi = (uint32_t) f;

     // decimal portion...get index for up to 3 decimal places.
     f = f - ((float) fi);
     f0 = f*10;   f0 %= 10;
     f1 = f*100;  f1 %= 10;
     f2 = f*1000; f2 %= 10;
     printf("%c%ld.%d%d%d", c, fi, (uint8_t) f0, (uint8_t) f1, (uint8_t) f2);
   }

Source: https://www.millennium.berkeley.edu/pipermail/tinyos-help/2008-June/034691.html

Clone this wiki locally