Skip to content

File syn_fmt.c

FileList > src > syntropic > util > syn_fmt.c

Go to the source code of this file

Lightweight formatting — no libc printf dependency.

  • #include "syn_fmt.h"

Public Static Attributes

Type Name
const char hex_chars = "0123456789ABCDEF"
Hex digit lookup table.

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
int hex_char_to_nibble (char c)
size_t write_char (char * buf, size_t size, size_t pos, char c)
Write a single char to the output buffer.
size_t write_str (char * buf, size_t size, size_t pos, const char * s)
Write a null-terminated string to the output buffer.

Public Static Attributes Documentation

variable hex_chars

Hex digit lookup table.

const char hex_chars[];


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 hex_char_to_nibble

static int hex_char_to_nibble (
    char c
) 

function write_char

Write a single char to the output buffer.

static size_t write_char (
    char * buf,
    size_t size,
    size_t pos,
    char c
) 

Parameters:

  • buf Output buffer.
  • size Buffer capacity.
  • pos Current write position.
  • c Character to write.

Returns:

New position (may exceed size if truncated).


function write_str

Write a null-terminated string to the output buffer.

static size_t write_str (
    char * buf,
    size_t size,
    size_t pos,
    const char * s
) 

Parameters:

  • buf Output buffer.
  • size Buffer capacity.
  • pos Current write position.
  • s String to write.

Returns:

New position.



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