[systemd-devel] [PATCH 5/6] Add hexstr function

Oleksii Shevchuk alxchk at gmail.com
Tue Feb 12 00:22:26 PST 2013


Function convers byte array to hex string
---
 src/shared/util.c | 23 +++++++++++++++++++++++
 src/shared/util.h |  1 +
 2 files changed, 24 insertions(+)

diff --git a/src/shared/util.c b/src/shared/util.c
index 24f9e7e..2a5cbbb 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -1368,6 +1368,29 @@ char hexchar(int x) {
         return table[x & 15];
 }
 
+char * hexstr (const uint8_t *in, size_t count)
+{
+        char *r = NULL, *i = NULL;
+
+        if (!in || !count)
+                goto finish;
+
+        r = i = new(char, count * 2 + 1);
+        if (! r)
+                goto finish;
+
+        while (count--) {
+                *i++ = hexchar(*in >> 4);
+                *i++ = hexchar(*in);
+                ++in;
+        }
+
+        *i = '\0';
+
+ finish:
+        return r;
+}
+
 int unhexchar(char c) {
 
         if (c >= '0' && c <= '9')
diff --git a/src/shared/util.h b/src/shared/util.h
index cd13457..fe85fa8 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -211,6 +211,7 @@ int get_process_uid(pid_t pid, uid_t *uid);
 int get_process_gid(pid_t pid, gid_t *gid);
 
 char hexchar(int x);
+char * hexstr (const uint8_t *in, size_t count);
 int unhexchar(char c);
 char octchar(int x);
 int unoctchar(char c);
-- 
1.8.1.2



More information about the systemd-devel mailing list