diff options
author | Loïc Collignon <loic.collignon@iot.bzh> | 2017-10-23 10:51:37 +0200 |
---|---|---|
committer | Loïc Collignon <loic.collignon@iot.bzh> | 2017-10-23 10:51:37 +0200 |
commit | 44096523e0c45c6b02840f2fe2aca337510fac28 (patch) | |
tree | dc9c7b2be0750c011fd7c581df65f7b2e0a9ec16 /src/stringutils.h | |
parent | 5441251cae0eea3786c327b3b3386eae5bf687db (diff) |
add nfc binding
Change-Id: I1ebf8e803436430490201db533c2a5a04c04295e
Signed-off-by: Loïc Collignon <loic.collignon@iot.bzh>
Diffstat (limited to 'src/stringutils.h')
-rw-r--r-- | src/stringutils.h | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/stringutils.h b/src/stringutils.h new file mode 100644 index 0000000..824d09d --- /dev/null +++ b/src/stringutils.h @@ -0,0 +1,41 @@ +#pragma once + +#include <stdlib.h> + +/** + * @brief Get a hexadecimal string representation from memory buffer. + * @param[in] src Buffer's pointer. + * @param[in] sz Buffer's size. + * @return A pointer to the result string. Caller is responsible for the result lifetime. + */ +static inline char* to_hex_string(const void* src, long unsigned int sz) +{ + static const char lookup[] = + { + '0', '1', '2', '3', + '4', '5', '6', '7', + '8', '9', 'a', 'b', + 'c', 'd', 'e', 'f' + }; + + const char* source; + char* result; + long unsigned int i; + + result = NULL; + if (src && sz) + { + source = (const char*)src; + result = (char*)malloc(sz * 2 + 1); + if (result) + { + result[sz * 2] = 0; + for (i = 0; i < sz; ++i) + { + result[i * 2] = lookup[(source[i] & 0xf0) >> 4]; + result[i * 2 + 1] = lookup[source[i] & 0x0f]; + } + } + } + return result; +} |