add hex_encode

This commit is contained in:
2024-03-28 19:09:39 -07:00
parent c795f607ef
commit f60bea0fdd
4 changed files with 40 additions and 5 deletions

View File

@ -23,16 +23,32 @@ in case you need to know the size of the decoded data, otherwise you can just pa
unsigned char *b64_decode(const char *s, size_t sz, size_t *decode_sz);
```
### hex_encode
Encodes an array of bytes into a string in hex representation. For example, converts
`[0xDE, 0xAD, 0xBE, 0xEF]` into `"DEADBEEF"`. The returned string will always be in capital
letters with no leading `0x`. User is responsible for the freeing of the returned string
```c
const char *hex_encode(const unsigned char *hex, size_t sz);
// Example
const char *s = hex_encode(h, 4);
assert(strcmp(s, "DEADBEEF") == 0);
free(s);
```
### hex_decode
Decodes a string of characters representing hexadecimal bytes into an array of `unsigned char`.
For example, converts `"DEADBEEF"` into `[0xDE, 0xAD, 0xBE, 0xEF]`.
For example, converts `"DEADBEEF"` into `[0xDE, 0xAD, 0xBE, 0xEF]`. User is reponsible for the
freeing of the returned byte array
```c
unsigned char *hex_decode(const char *orig, size_t *sz);
// Example
s = hex_decode("DEADBEEF", &s_sz);
unsigned char *s = hex_decode("DEADBEEF", &s_sz);
unsigned char h[4] = {
0xDE, 0xAD, 0xBE, 0xEF
};