X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fstrutil.c;h=93e424ee1f047d7ba96a23c4b657450597a2c94c;hb=d8bc7ca3e6853bd4df33d9ff9405652abec8b67e;hp=73c9fc6af4a85459b75c659ede5421c2ac3f6688;hpb=08f8421a9e82fec8bb2fa4e561fea8cb468abcca;p=libsigrok.git diff --git a/src/strutil.c b/src/strutil.c index 73c9fc6a..93e424ee 100644 --- a/src/strutil.c +++ b/src/strutil.c @@ -590,6 +590,42 @@ SR_API int sr_vsnprintf_ascii(char *buf, size_t buf_size, #endif } +/** + * Convert a sequence of bytes to its textual representation ("hex dump"). + * + * Callers should free the allocated GString. See @ref sr_hexdump_free(). + * + * @param[in] data Pointer to the byte sequence to print. + * @param[in] len Number of bytes to print. + * + * @return #NULL upon error, newly allocated GString pointer otherwise. + */ +SR_PRIV GString *sr_hexdump_new(const uint8_t *data, const size_t len) +{ + GString *s; + size_t i; + + s = g_string_sized_new(3 * len); + for (i = 0; i < len; i++) { + if (i) + g_string_append_c(s, ' '); + g_string_append_printf(s, "%02x", data[i]); + } + + return s; +} + +/** + * Free a hex dump text that was created by @ref sr_hexdump_new(). + * + * @param[in] s Pointer to the GString to release. + */ +SR_PRIV void sr_hexdump_free(GString *s) +{ + if (s) + g_string_free(s, TRUE); +} + /** * Convert a string representation of a numeric value to a sr_rational. *