From 86d2648ccad546ddf5f0fea7847ef66fa41a8d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Tue, 11 Aug 2026 23:36:48 +0200 Subject: [PATCH] Add hex encoding/decoding --- src/main.c | 28 ++++++++++++++++++++++++++++ src/string.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/string.h | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 113 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index df8a0fe..3294857 100644 --- a/src/main.c +++ b/src/main.c @@ -533,6 +533,33 @@ void string_test_utf8(void) TRACEF("str4:\n"); print_runes(str4); // this should be a valid code point +} + +void string_test_hex(void) +{ + MArena arena; + U8 data[4096]; + marena_init(&arena, data, sizeof data); + Allocator allocator = marena_allocator(&arena); + + Str str1 = S("Hello World ☺"); + + Str str1_encoded = str_hex_encode_alloc(&allocator, str1); + printf("str1_encoded: '%.*s'\n", str1_encoded.len, str1_encoded.str); + + Str str1_decoded = str_hex_decode_alloc(&allocator, str1_encoded); + printf("str1_decoded: '%.*s'\n", str1_decoded.len, str1_decoded.str); + + Str str2 = S(""); + + Str str2_encoded = str_hex_encode_alloc(&allocator, str2); + printf("str2_encoded: '%.*s'\n", str2_encoded.len, str2_encoded.str); + + Str str2_decoded = str_hex_decode_alloc(&allocator, str2_encoded); + printf("str2_decoded: '%.*s'\n", str2_decoded.len, str2_decoded.str); + + + } int main(int argc, char *argv[]) @@ -552,6 +579,7 @@ int main(int argc, char *argv[]) string_test_misc(); string_test_join(); string_test_utf8(); + string_test_hex(); } diff --git a/src/string.c b/src/string.c index 3d4d89d..a31fa5d 100644 --- a/src/string.c +++ b/src/string.c @@ -93,6 +93,58 @@ S64 str_index(Str haystack, Str needle) return -1; } +static const char hex_chars[] = "0123456789ABCDEF"; + +Str str_hex_encode(Str source, Str destination) +{ + Assert(destination.len >= source.len * 2); + U32 hex_len = source.len * 2; + for (U32 i = 0, t = 0; i < source.len; ++i,t+=2) { + U8 high_nibble; + U8 low_nibble; + + high_nibble = (source.str[i] & 0xF0) >> 4 ; + low_nibble = source.str[i] & 0x0F ; + + destination.str[t] = hex_chars[high_nibble]; + destination.str[t+1] = hex_chars[low_nibble]; + } + + destination.len = hex_len; + return destination; +} + +Str str_hex_decode(Str source, Str destination) +{ + Assert(destination.len >= source.len/2); + U32 dest_pos = 0; + U32 source_pos = 0; + + while (source_pos + 1 < source.len) { + + if (char_is_ascii_whitespace(source.str[source_pos])) { + source_pos++; + continue; + } + + S16 high_nibble; + S16 low_nibble; + + high_nibble = char_hex_to_binary(source.str[source_pos]); + low_nibble = char_hex_to_binary(source.str[source_pos + 1]); + if (high_nibble == -1 || low_nibble == -1) { + destination.len = 0; + return destination; + } + + destination.str[dest_pos] = (high_nibble << 4 ) | low_nibble; + source_pos += 2; + dest_pos++; + } + + destination.len = dest_pos; + return destination; +} // split @str on any byte in @separators, place split strings in @vec void str_split(Str str, Str separators, StrVec *vec) { diff --git a/src/string.h b/src/string.h index 0479918..6092e9b 100644 --- a/src/string.h +++ b/src/string.h @@ -91,7 +91,29 @@ static INLINE Str str_skip(Str str, U32 len) return new_str; } +// Destination must be at least source.len * 2. Returns destination. +Str str_hex_encode(Str source, Str destination); +// Destination must be at least source.len / 2. Returns destination. +Str str_hex_decode(Str source, Str destination); +static INLINE Str str_hex_encode_alloc(Allocator *allocator, Str str) +{ + Assert(str.len <= U32_Max / 2); + U8 hex_len = str.len * 2; + U8 *result = allocator_push_array(allocator, U8, hex_len); + Str destination = str_create(result, hex_len); + + return str_hex_encode(str, destination); +} + +static INLINE Str str_hex_decode_alloc(Allocator *allocator, Str str) +{ + U8 binary_len = str.len / 2; + U8 *result = allocator_push_array(allocator, U8, binary_len); + Str destination = str_create(result, binary_len); + + return str_hex_decode(str, destination); +} // String Vector typedef struct StrVec StrVec; struct StrVec { @@ -113,11 +135,11 @@ void str_split(Str str, Str separators, StrVec *vec); Str str_join(Allocator *allocator, StrVec strings, Str separator); +// Characters static INLINE bool char_is_ascii_whitespace(U8 byte) { return byte == ' ' || byte == '\t' || byte == '\v' || byte == '\f' || byte == '\r' || byte == '\n'; - } static INLINE bool char_is_digit(U8 byte) @@ -125,7 +147,17 @@ static INLINE bool char_is_digit(U8 byte) return byte >= '0' && byte <= '9'; } +static INLINE S16 char_hex_to_binary(U8 c) +{ + if (c >='0' && c <='9') + return c - '0'; + else if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + else if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + return -1; +} // UTF-8/Unicode