]> pilppa.org Git - libplp.git/commitdiff
utils for getting byte array as hex str.
authorMika Laitio <lamikr@pilppa.org>
Mon, 14 Feb 2011 20:31:02 +0000 (22:31 +0200)
committerMika Laitio <lamikr@pilppa.org>
Mon, 14 Feb 2011 20:31:02 +0000 (22:31 +0200)
Signed-off-by: Mika Laitio <lamikr@pilppa.org>
src/str_util.c [new file with mode: 0644]
src/str_util.h [new file with mode: 0644]

diff --git a/src/str_util.c b/src/str_util.c
new file mode 100644 (file)
index 0000000..aca2fd8
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * str.c
+ *
+ *  Created on: Feb 14, 2011
+ *      Author: lamikr
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+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 (file)
index 0000000..2a1a599
--- /dev/null
@@ -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_ */