String formatting (fmt)¶
Provides simple string formatting functions.
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.
-
msp430_types.h::size_tfmt_byte_hex(char * out, uint8_t byte)¶ Format a byte value as hex.
E.g., converts byte value 0 to the string 00, 255 to the string FF.
Will write two bytes to
out. Ifoutis NULL, will only return the number of bytes that would have been written.Parameters
out: Pointer to output buffer, or NULL byte: Byte value to convert Return values
- 2
-
msp430_types.h::size_tfmt_bytes_hex(char * out, const uint8_t * ptr,msp430_types.h::size_tn)¶ Formats a sequence of bytes as hex bytes.
Will write 2*n bytes to
out. Ifoutis NULL, will only return the number of bytes that would have been written.Parameters
out: Pointer to output buffer, or NULL ptr: Pointer to bytes to convert n: Number of bytes to convert Return values
- 2*n
-
msp430_types.h::size_tfmt_bytes_hex_reverse(char * out, const uint8_t * ptr,msp430_types.h::size_tn)¶ Formats a sequence of bytes as hex bytes, starting with the last byte.
Will write 2*n bytes to
out. Ifoutis NULL, will only return the number of bytes that would have been written.Parameters
out: Pointer to output buffer, or NULL ptr: Pointer to bytes to convert n: Number of bytes to convert Return values
- 2*n
-
uint8_t
fmt_hex_byte(const char * hex)¶ Converts a sequence of two hex characters to a byte.
The hex characters sequence must contain valid hexadecimal characters otherwise the result is undefined.
Parameters
hex: Pointer to input buffer Return values
- byte based on hex string
-
msp430_types.h::size_tfmt_hex_bytes(uint8_t * out, const char * hex)¶ Converts a sequence of hex bytes to an array of bytes.
The sequence of hex characters must have an even length: 2 hex character => 1 byte. If the sequence of hex has an odd length, this function returns 0 and an empty
out.The hex characters sequence must contain valid hexadecimal characters otherwise the result in
outis undefined.Parameters
out: Pointer to converted bytes, or NULL hex: Pointer to input buffer Return values
- strlen(hex) / 2 when length of
hexwas even - 0 otherwise
- strlen(hex) / 2 when length of
-
msp430_types.h::size_tfmt_u16_hex(char * out, uint16_t val)¶ Convert a uint16 value to hex string.
Will write 4 bytes to
out. Ifoutis NULL, will only return the number of bytes that would have been written.Parameters
out: Pointer to output buffer, or NULL val: Value to convert Return values
- 4
-
msp430_types.h::size_tfmt_u32_hex(char * out, uint32_t val)¶ Convert a uint32 value to hex string.
Will write 8 bytes to
out. Ifoutis NULL, will only return the number of bytes that would have been written.Parameters
out: Pointer to output buffer, or NULL val: Value to convert Return values
- 8
-
msp430_types.h::size_tfmt_u64_hex(char * out, uint64_t val)¶ Convert a uint64 value to hex string.
Will write 16 bytes to
out. Ifoutis NULL, will only return the number of bytes that would have been written.Parameters
out: Pointer to output buffer, or NULL val: Value to convert Return values
- 16
-
msp430_types.h::size_tfmt_u32_dec(char * out, uint32_t val)¶ Convert a uint32 value to decimal string.
If
outis NULL, will only return the number of bytes that would have been written.Parameters
out: Pointer to output buffer, or NULL val: Value to convert Return values
- nr of digits written to (or needed in)
out
- nr of digits written to (or needed in)
-
msp430_types.h::size_tfmt_u64_dec(char * out, uint64_t val)¶ Convert a uint64 value to decimal string.
If
outis NULL, will only return the number of bytes that would have been written.Note
This adds ~400b of code when used.
Parameters
out: Pointer to output buffer, or NULL val: Value to convert Return values
- nr of digits written to (or needed in)
out
- nr of digits written to (or needed in)
-
msp430_types.h::size_tfmt_u16_dec(char * out, uint16_t val)¶ Convert a uint16 value to decimal string.
If
outis NULL, will only return the number of bytes that would have been written.Parameters
out: Pointer to output buffer, or NULL val: Value to convert Return values
- nr of digits written to (or needed in)
out
- nr of digits written to (or needed in)
-
msp430_types.h::size_tfmt_s64_dec(char * out, int64_t val)¶ Convert a int64 value to decimal string.
Will add a leading “-” if
valis negative.If
outis NULL, will only return the number of bytes that would have been written.Parameters
out: Pointer to output buffer, or NULL val: Value to convert Return values
- nr of characters written to (or needed in)
out
- nr of characters written to (or needed in)
-
msp430_types.h::size_tfmt_s32_dec(char * out, int32_t val)¶ Convert a int32 value to decimal string.
Will add a leading “-” if
valis negative.If
outis NULL, will only return the number of bytes that would have been written.Parameters
out: Pointer to output buffer, or NULL val: Value to convert Return values
- nr of characters written to (or needed in)
out
- nr of characters written to (or needed in)
-
msp430_types.h::size_tfmt_s16_dec(char * out, int16_t val)¶ Convert a int16 value to decimal string.
Will add a leading “-” if
valis negative.If
outis NULL, will only return the number of bytes that would have been written.Parameters
out: Pointer to output buffer, or NULL val: Value to convert Return values
- nr of characters written to (or needed in)
out
- nr of characters written to (or needed in)
-
msp430_types.h::size_tfmt_s16_dfp(char * out, int16_t val, int fp_digits)¶ Convert 16-bit fixed point number to a decimal string.
See
fmt.h::fmt_s32_dfp()for more detailsParameters
out: Pointer to the output buffer, or NULL val: Fixed point value fp_digits: Number of digits after the decimal point, MUST be >= -7 Return values
- Length of the resulting string
-
msp430_types.h::size_tfmt_s32_dfp(char * out, int32_t val, int fp_digits)¶ Convert 32-bit fixed point number to a decimal string.
The input for this function is a signed 32-bit integer holding the fixed point value as well as an integer defining the position of the decimal point. This value is used to shift the decimal point to the right (positive value of
fp_digits) or to the left (negative value offp_digits).Will add a leading “-” if
valis negative.The resulting string will always be patted with zeros after the decimal point.
For example: if
valis -3548 andfp_digitsis -2, the resulting string will be “-35.48”. The same value forvalwithfp_digitsof 2 will result in “-354800”.Parameters
out: Pointer to the output buffer, or NULL val: Fixed point value fp_digits: Number of digits after the decimal point, MUST be >= -7 Return values
- Length of the resulting string
-
msp430_types.h::size_tfmt_float(char * out, float f, unsigned precision)¶ Format float to string.
Converts float value
fto stringIf
outis NULL, will only return the number of bytes that would have been written.Parameters
out: string to write to (or NULL) f: float value to convert precision: number of digits after decimal point Return values
- nr of bytes the function did or would write to out
-
msp430_types.h::size_tfmt_char(char * out, char c)¶ Copy
inchar to string (without terminating ‘\0’)If
outis NULL, will only return the number of bytes that would have been written.Parameters
out: string to write to (or NULL) c: char value to append Return values
- nr of bytes the function did or would write to out
-
msp430_types.h::size_tfmt_strlen(const char * str)¶ Count characters until ‘\0’ (exclusive) in
str.Parameters
str: Pointer to string Return values
- nr of characters in string
strpoints to
- nr of characters in string
-
msp430_types.h::size_tfmt_strnlen(const char * str,msp430_types.h::size_tmaxlen)¶ Count at most
maxlencharacters until ‘\0’ (exclusive) instr.Parameters
str: Pointer to string maxlen: Maximum number of chars to count Return values
- nr of characters in string
strpoints to, ormaxlenif no null terminator is found withinmaxlenchars
- nr of characters in string
-
msp430_types.h::size_tfmt_str(char * out, const char * str)¶ Copy null-terminated string (excluding terminating \0)
If
outis NULL, will only return the number of bytes that would have been written.Parameters
out: Pointer to output buffer, or NULL str: Pointer to null-terminated source string Return values
- nr of characters written to (or needed in)
out
- nr of characters written to (or needed in)
-
msp430_types.h::size_tfmt_to_lower(char * out, const char * str)¶ Copy null-terminated string to a lowercase string (excluding terminating \0)
Parameters
out: Pointer to output buffer, or NULL str: Pointer to null-terminated source string
-
uint32_t
scn_u32_dec(const char * str,msp430_types.h::size_tn)¶ Convert digits to uint32.
Will convert up to
ndigits. Stops at any non-digit or ‘\0’ character.Parameters
str: Pointer to string to read from n: Maximum nr of characters to consider Return values
- converted uint32_t value
-
uint32_t
scn_u32_hex(const char * str,msp430_types.h::size_tn)¶ Convert hexadecimal characters to uin32_t.
Will convert up to
nchar. Stop at any non-hexadecimal or ‘\0’ characterParameters
str: Pointer to tring to read from n: Maximum number of characters to consider Return values
- converted uint32_t value
-
void
print(const char * s,msp430_types.h::size_tn)¶ Print string to stdout.
Writes
nbytes fromsto STDOUT_FILENO.Parameters
s: Pointer to string to print n: Number of bytes to print
-
void
print_u32_dec(uint32_t val)¶ Print uint32 value to stdout.
Parameters
val: Value to print
-
void
print_s32_dec(int32_t val)¶ Print int32 value to stdout.
Parameters
val: Value to print
-
void
print_byte_hex(uint8_t byte)¶ Print byte value as hex to stdout.
Parameters
byte: Byte value to print
-
void
print_u32_hex(uint32_t val)¶ Print uint32 value as hex to stdout.
Parameters
val: Value to print
-
void
print_u64_hex(uint64_t val)¶ Print uint64 value as hex to stdout.
Parameters
val: Value to print
-
void
print_u64_dec(uint64_t val)¶ Print uint64 value as decimal to stdout.
Note
This uses
fmt.h::fmt_u64_dec(), which uses ~400b of code.Parameters
val: Value to print
-
void
print_float(float f, unsigned precision)¶ Print float value.
Note
See fmt_float for code size warning!
Parameters
f: float value to print precision: number of digits after decimal point
-
void
print_str(const char * str)¶ Print null-terminated string to stdout.
Parameters
str: Pointer to string to print
-
msp430_types.h::size_tfmt_lpad(char * str,msp430_types.h::size_tin_len,msp430_types.h::size_tpad_len, char pad_char)¶ Pad string to the left.
This function left-pads a given string
strwithpad_char.For example, calling
would result in ” abcd”.
The function only writes to
strif str is non-NULL andpad_lenis <in_len.Note
Caller must ensure
strcan take pad_len characters!Parameters
str: string to pad (or NULL) in_len: length of str pad_len: total length after padding pad_char: char to use as pad char Return values
- max(in_len, pad_len)
-
FMT_USE_MEMMOVE¶ use memmove() or internal implementation
1
(1)