Skip to content

File syn_fmt.h

FileList > src > syntropic > util > syn_fmt.h

Go to the source code of this file

Lightweight formatting — no libc printf dependency. More...

  • #include <stdbool.h>
  • #include <stddef.h>
  • #include <stdint.h>

Public Functions

Type Name
size_t syn_fmt_concat (char * buf, size_t size, const char *const * parts, size_t n)
Build a string from parts (like snprintf but simpler).
size_t syn_fmt_fixed (char * buf, size_t size, int32_t val, uint8_t places)
Format a value with a fixed number of decimal places.
size_t syn_fmt_hex (char * buf, size_t size, uint32_t val, uint8_t min_digits)
Format a value as hex.
size_t syn_fmt_hex_parse (const char * hex_str, uint8_t * out_bin, size_t max_bytes)
Parse a hex string into a binary byte array.
size_t syn_fmt_hexdump (char * buf, size_t size, const uint8_t * data, size_t len)
Format a hex dump of a byte array.
size_t syn_fmt_int (char * buf, size_t size, int32_t val)
Format a signed integer to decimal string.
size_t syn_fmt_q16 (char * buf, size_t size, int32_t q16_val, uint8_t frac_digits)
Format a Q16.16 fixed-point value.
size_t syn_fmt_uint (char * buf, size_t size, uint32_t val)
Format an unsigned integer to decimal string.

Public Static Functions

Type Name
uint32_t syn_fmt_parse_uint (const char * s)
Parse a decimal integer from a string.
bool syn_str_prefix_icase (const char * str, const char * prefix)
Case-insensitive prefix match (ASCII).

Detailed Description

Integer-to-string, hex, fixed-point, and hex dump. All functions write to a caller-provided buffer and return the number of chars written (excluding null terminator).

** **

char buf[16];
syn_fmt_int(buf, sizeof(buf), -1234);     // "-1234"
syn_fmt_hex(buf, sizeof(buf), 0xDEAD, 4); // "DEAD"
syn_fmt_q16(buf, sizeof(buf), val, 3);    // "3.141"

Public Functions Documentation

function syn_fmt_concat

Build a string from parts (like snprintf but simpler).

size_t syn_fmt_concat (
    char * buf,
    size_t size,
    const char *const * parts,
    size_t n
) 

Concatenate up to n string fragments into buf.

Parameters:

  • buf Output buffer.
  • size Buffer capacity.
  • parts Array of string pointers.
  • n Number of strings.

Returns:

Total characters written.


function syn_fmt_fixed

Format a value with a fixed number of decimal places.

size_t syn_fmt_fixed (
    char * buf,
    size_t size,
    int32_t val,
    uint8_t places
) 

E.g., syn_fmt_fixed(buf, 16, 12345, 3) → "12.345"

Parameters:

  • buf Output buffer.
  • size Buffer capacity in bytes.
  • val Integer value.
  • places Number of decimal places from the right.

Returns:

Number of characters written.


function syn_fmt_hex

Format a value as hex.

size_t syn_fmt_hex (
    char * buf,
    size_t size,
    uint32_t val,
    uint8_t min_digits
) 

Parameters:

  • buf Output buffer.
  • size Buffer capacity in bytes.
  • val Value to format.
  • min_digits Minimum hex digits (zero-padded).

Returns:

Number of characters written.


function syn_fmt_hex_parse

Parse a hex string into a binary byte array.

size_t syn_fmt_hex_parse (
    const char * hex_str,
    uint8_t * out_bin,
    size_t max_bytes
) 

Supports both uppercase and lowercase hex characters.

Parameters:

  • hex_str Input hex string.
  • out_bin [out] Output binary byte buffer.
  • max_bytes Capacity of out_bin in bytes.

Returns:

Number of binary bytes parsed.


function syn_fmt_hexdump

Format a hex dump of a byte array.

size_t syn_fmt_hexdump (
    char * buf,
    size_t size,
    const uint8_t * data,
    size_t len
) 

Output: "DE AD BE EF" (space-separated hex bytes).

Parameters:

  • buf Output buffer.
  • size Buffer capacity.
  • data Byte array.
  • len Number of bytes.

Returns:

Number of characters written.


function syn_fmt_int

Format a signed integer to decimal string.

size_t syn_fmt_int (
    char * buf,
    size_t size,
    int32_t val
) 

Parameters:

  • buf Output buffer.
  • size Buffer capacity.
  • val Value to format.

Returns:

Number of characters written (excluding null).


function syn_fmt_q16

Format a Q16.16 fixed-point value.

size_t syn_fmt_q16 (
    char * buf,
    size_t size,
    int32_t q16_val,
    uint8_t frac_digits
) 

Parameters:

  • buf Output buffer.
  • size Buffer capacity in bytes.
  • q16_val Q16.16 value to format.
  • frac_digits Number of fractional decimal digits (1–6).

Returns:

Number of characters written.


function syn_fmt_uint

Format an unsigned integer to decimal string.

size_t syn_fmt_uint (
    char * buf,
    size_t size,
    uint32_t val
) 

Parameters:

  • buf Output buffer.
  • size Buffer capacity.
  • val Value to format.

Returns:

Number of characters written (excluding null).


Public Static Functions Documentation

function syn_fmt_parse_uint

Parse a decimal integer from a string.

static inline uint32_t syn_fmt_parse_uint (
    const char * s
) 

Parameters:

  • s Null-terminated decimal string.

Returns:

Parsed unsigned 32-bit integer.


function syn_str_prefix_icase

Case-insensitive prefix match (ASCII).

static inline bool syn_str_prefix_icase (
    const char * str,
    const char * prefix
) 

Parameters:

  • str String to test.
  • prefix Prefix to match.

Returns:

true if str starts with prefix (case-insensitive).



The documentation for this class was generated from the following file src/syntropic/util/syn_fmt.h