The friendly Operating System for the Internet of Things
String formatting (fmt)

Provides simple string formatting functions. More...

Detailed Description

The goal of this API is to provide a string formatting interface which has a reduced code size footprint compared to the libc provided stdio.h functionality.

This library provides a set of formatting and printing functions for 64 bit integers, even when the C library was built without support for 64 bit formatting (newlib-nano).

Note
The print functions in this library do not buffer any output. Mixing calls to standard printf from stdio.h with the print_xxx functions in fmt, especially on the same output line, may cause garbled output.

Files

file  fmt.h
 String formatting API.
 

Functions

size_t fmt_byte_hex (char *out, uint8_t byte)
 Format a byte value as hex. More...
 
size_t fmt_bytes_hex_reverse (char *out, const uint8_t *ptr, size_t n)
 Formats a sequence of bytes as hex bytes, starting with the last byte. More...
 
size_t fmt_u32_hex (char *out, uint32_t val)
 Convert a uint32 value to hex string. More...
 
size_t fmt_u64_hex (char *out, uint64_t val)
 Convert a uint64 value to hex string. More...
 
size_t fmt_u32_dec (char *out, uint32_t val)
 Convert a uint32 value to decimal string. More...
 
size_t fmt_u64_dec (char *out, uint64_t val)
 Convert a uint64 value to decimal string. More...
 
size_t fmt_u16_dec (char *out, uint16_t val)
 Convert a uint16 value to decimal string. More...
 
size_t fmt_s32_dec (char *out, int32_t val)
 Convert a int32 value to decimal string. More...
 
size_t fmt_s16_dec (char *out, int16_t val)
 Convert a int16 value to decimal string. More...
 
size_t fmt_s16_dfp (char *out, int16_t val, unsigned fp_digits)
 Convert 16-bit fixed point number to a decimal string. More...
 
size_t fmt_strlen (const char *str)
 Count characters until '\0' (exclusive) in str. More...
 
size_t fmt_str (char *out, const char *str)
 Copy null-terminated string (excluding terminating \0) More...
 
uint32_t scn_u32_dec (const char *str, size_t n)
 Convert digits to uint32. More...
 
void print (const char *s, size_t n)
 Print string to stdout. More...
 
void print_u32_dec (uint32_t val)
 Print uint32 value to stdout. More...
 
void print_s32_dec (int32_t val)
 Print int32 value to stdout. More...
 
void print_byte_hex (uint8_t byte)
 Print byte value as hex to stdout. More...
 
void print_u32_hex (uint32_t val)
 Print uint32 value as hex to stdout. More...
 
void print_u64_hex (uint64_t val)
 Print uint64 value as hex to stdout. More...
 
void print_u64_dec (uint64_t val)
 Print uint64 value as decimal to stdout. More...
 
void print_str (const char *str)
 Print null-terminated string to stdout. More...
 

Function Documentation

size_t fmt_byte_hex ( char *  out,
uint8_t  byte 
)

E.g., converts byte value 0 to the string 00, 255 to the string FF.

Will write two bytes to out. If out is NULL, will only return the number of bytes that would have been written.

Parameters
[out]outPointer to output buffer, or NULL
[in]byteByte value to convert
Returns
2
size_t fmt_bytes_hex_reverse ( char *  out,
const uint8_t *  ptr,
size_t  n 
)

Will write 2*n bytes to out. If out is NULL, will only return the number of bytes that would have been written.

Parameters
[out]outPointer to output buffer, or NULL
[in]ptrPointer to bytes to convert
[in]nNumber of bytes to convert
Returns
2*n
size_t fmt_s16_dec ( char *  out,
int16_t  val 
)

Will add a leading "-" if val is negative.

If out is NULL, will only return the number of bytes that would have been written.

Parameters
[out]outPointer to output buffer, or NULL
[in]valValue to convert
Returns
nr of characters written to (or needed in) out
size_t fmt_s16_dfp ( char *  out,
int16_t  val,
unsigned  fp_digits 
)

The input for this function is a signed 16-bit integer holding the fixed point value as well as an unsigned integer defining the position of the decimal point, so this value defines the number of decimal digits after the decimal point.

The resulting string will always be patted with zeros after the decimal point.

For example: if val is -3548 and fp_digits is 2, the resulting string will be "-35.48". For val := 12010 and fp_digits := 3 the result will be "12.010".

Will add a leading "-" if val is negative.

If out is NULL, will only return the number of bytes that would have been written.

Parameters
[out]outPointer to the output buffer, or NULL
[in]valFixed point value, MUST be <= 4
[in]fp_digitsNumber of digits after the decimal point
Returns
Length of the resulting string
0 if fp_digits is > 4
size_t fmt_s32_dec ( char *  out,
int32_t  val 
)

Will add a leading "-" if val is negative.

If out is NULL, will only return the number of bytes that would have been written.

Parameters
[out]outPointer to output buffer, or NULL
[in]valValue to convert
Returns
nr of characters written to (or needed in) out
size_t fmt_str ( char *  out,
const char *  str 
)

If out is NULL, will only return the number of bytes that would have been written.

Parameters
[out]outPointer to output buffer, or NULL
[in]strPointer to null-terminated source string
Returns
nr of characters written to (or needed in) out
size_t fmt_strlen ( const char *  str)
Parameters
[in]strPointer to string
Returns
nr of characters in string str points to
size_t fmt_u16_dec ( char *  out,
uint16_t  val 
)

If out is NULL, will only return the number of bytes that would have been written.

Parameters
[out]outPointer to output buffer, or NULL
[in]valValue to convert
Returns
nr of digits written to (or needed in) out
size_t fmt_u32_dec ( char *  out,
uint32_t  val 
)

If out is NULL, will only return the number of bytes that would have been written.

Parameters
[out]outPointer to output buffer, or NULL
[in]valValue to convert
Returns
nr of digits written to (or needed in) out
size_t fmt_u32_hex ( char *  out,
uint32_t  val 
)

Will write 8 bytes to out. If out is NULL, will only return the number of bytes that would have been written.

Parameters
[out]outPointer to output buffer, or NULL
[in]valValue to convert
Returns
8
size_t fmt_u64_dec ( char *  out,
uint64_t  val 
)

If out is NULL, will only return the number of bytes that would have been written.

Note
This adds ~400b of code when used.
Parameters
[out]outPointer to output buffer, or NULL
[in]valValue to convert
Returns
nr of digits written to (or needed in) out
size_t fmt_u64_hex ( char *  out,
uint64_t  val 
)

Will write 16 bytes to out. If out is NULL, will only return the number of bytes that would have been written.

Parameters
[out]outPointer to output buffer, or NULL
[in]valValue to convert
Returns
16
void print ( const char *  s,
size_t  n 
)

Writes n bytes from s to STDOUT_FILENO.

Parameters
[out]sPointer to string to print
[in]nNumber of bytes to print
void print_byte_hex ( uint8_t  byte)
Parameters
[in]byteByte value to print
void print_s32_dec ( int32_t  val)
Parameters
[in]valValue to print
void print_str ( const char *  str)
Parameters
[in]strPointer to string to print
void print_u32_dec ( uint32_t  val)
Parameters
[in]valValue to print
void print_u32_hex ( uint32_t  val)
Parameters
[in]valValue to print
void print_u64_dec ( uint64_t  val)
Note
This uses fmt_u64_dec(), which uses ~400b of code.
Parameters
[in]valValue to print
void print_u64_hex ( uint64_t  val)
Parameters
[in]valValue to print
uint32_t scn_u32_dec ( const char *  str,
size_t  n 
)

Will convert up to n digits. Stops at any non-digit or '\0' character.

Parameters
[in]strPointer to string to read from
[in]nMaximum nr of characters to consider
Returns
converted uint32_t value