From 1e9f874ec2f1072c1916db24d59b040eb0fe93cc Mon Sep 17 00:00:00 2001 From: Mika Laitio Date: Mon, 14 Feb 2011 22:31:02 +0200 Subject: [PATCH] utils for getting byte array as hex str. Signed-off-by: Mika Laitio --- src/str_util.c | 35 +++++++++++++++++++++++++++++++++++ src/str_util.h | 13 +++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/str_util.c create mode 100644 src/str_util.h diff --git a/src/str_util.c b/src/str_util.c new file mode 100644 index 0000000..aca2fd8 --- /dev/null +++ b/src/str_util.c @@ -0,0 +1,35 @@ +/* + * str.c + * + * Created on: Feb 14, 2011 + * Author: lamikr + */ + +#include +#include +#include + +char *get_as_hex_str(const char *byte_arr, int byte_count) { + int ii; + char *ret_val; + char item[5]; + + ret_val = NULL; + if ((byte_arr != NULL) && + (byte_count > 0)) { + ret_val = malloc(5 * byte_count); + for (ii = 0; ii < byte_count; ii++) { + if (ii == 0) { + snprintf(ret_val, 5, "0x%02x", byte_arr[ii]); + } + else { + snprintf(item, 5, "0x%02x", byte_arr[ii]); + strcat(ret_val, item); + } + if (ii < (byte_count - 1)) { + strcat(ret_val, " "); + } + } + } + return ret_val; +} diff --git a/src/str_util.h b/src/str_util.h new file mode 100644 index 0000000..2a1a599 --- /dev/null +++ b/src/str_util.h @@ -0,0 +1,13 @@ +/* + * str_util.h + * + * Created on: Feb 14, 2011 + * Author: lamikr + */ + +#ifndef STR_UTIL_H_ +#define STR_UTIL_H_ + +char *get_as_hex_str(const char *byte_arr, int byte_count); + +#endif /* STR_UTIL_H_ */ -- 2.41.0