Feature/bacnet-str-itoa-helpers (#1157)

* Added BACnet library itoa, ltoa, ultoa, dtoa, and utoa ASCII functions for ease of printing formatted values.

* Exposed the bacnet_byte_reverse_bits() function API.
This commit is contained in:
Steve Karg
2025-11-21 11:30:44 -06:00
committed by GitHub
parent 004aaf702d
commit 2a2407b2c6
5 changed files with 106 additions and 6 deletions
+82
View File
@@ -1775,3 +1775,85 @@ bool bacnet_string_to_unsigned(
return true;
}
/**
* @brief General purpose print formatter which returns the formatted string
* @param buffer - destination string
* @param count - length of the destination string
* @param format - format string
* @return character string buffer
*/
char *
bacnet_sprintf_to_ascii(char *buffer, size_t size, const char *format, ...)
{
va_list args;
va_start(args, format);
/* The vsnprintf function always writes a null terminator,
even if it truncates the output. */
(void)vsnprintf(buffer, size, format, args);
va_end(args);
return buffer;
}
/**
* @brief Convert a value into a string and return the base-10 string
* @param value Number to be converted.
* @param buffer Buffer that holds the result of the conversion.
* @param size Length of the buffer in units of the character type.
* @param precision Number of decimal places
* @return character string buffer
*/
char *bacnet_dtoa(double value, char *buffer, size_t size, unsigned precision)
{
return bacnet_sprintf_to_ascii(buffer, size, "%.*f", precision, value);
}
/**
* @brief Convert a value into a string and return the base-10 string
* @param value Number to be converted.
* @param buffer Buffer that holds the result of the conversion.
* @param size Length of the buffer in units of the character type.
* @return character string buffer
*/
char *bacnet_itoa(int value, char *buffer, size_t size)
{
return bacnet_sprintf_to_ascii(buffer, size, "%d", value);
}
/**
* @brief Convert a value into a string and return the base-10 string
* @param value Number to be converted.
* @param buffer Buffer that holds the result of the conversion.
* @param size Length of the buffer in units of the character type.
* @return character string buffer
*/
char *bacnet_ltoa(long value, char *buffer, size_t size)
{
return bacnet_sprintf_to_ascii(buffer, size, "%ld", value);
}
/**
* @brief Convert a value into a string and return the base-10 string
* @param value Number to be converted.
* @param buffer Buffer that holds the result of the conversion.
* @param size Length of the buffer in units of the character type.
* @return character string buffer
*/
char *bacnet_utoa(unsigned value, char *buffer, size_t size)
{
return bacnet_sprintf_to_ascii(buffer, size, "%u", value);
}
/**
* @brief Convert a value into a string and return the base-10 string
* @param value Number to be converted.
* @param buffer Buffer that holds the result of the conversion.
* @param size Length of the buffer in units of the character type.
* @return character string buffer
*/
char *bacnet_ultoa(unsigned long value, char *buffer, size_t size)
{
return bacnet_sprintf_to_ascii(buffer, size, "%lu", value);
}