Skip to content

Commit e00b2ea

Browse files
Add utility function array_bytes_string
1 parent 1056f35 commit e00b2ea

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/common_utils.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@
2323
#include "lcx_ecfp.h"
2424
#include "lcx_sha3.h"
2525

26+
int array_bytes_string(char *out, size_t outl, const void *value, size_t len) {
27+
if (outl <= 2) {
28+
// Need at least '0x' and 1 digit
29+
return -1;
30+
}
31+
if (strlcpy(out, "0x", outl) != 2) {
32+
goto err;
33+
}
34+
if (format_hex(value, len, out + 2, outl - 2) < 0) {
35+
goto err;
36+
}
37+
return 0;
38+
err:
39+
*out = '\0';
40+
return -1;
41+
}
42+
2643
uint64_t u64_from_BE(const uint8_t *in, uint8_t size) {
2744
uint8_t i = 0;
2845
uint64_t res = 0;

src/common_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ DEPRECATED static inline void array_hexstr(char *strbuf, const void *bin, unsign
4646
format_hex(bin, len, strbuf, (2 * len + 1));
4747
}
4848

49+
int array_bytes_string(char *out, size_t outl, const void *value, size_t len);
50+
4951
uint64_t u64_from_BE(const uint8_t *in, uint8_t size);
5052

5153
bool u64_to_string(uint64_t src, char *dst, uint8_t dst_size);

0 commit comments

Comments
 (0)