mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 03:50:57 +01:00
added _hex2bin()
This commit is contained in:
@@ -83,3 +83,39 @@ char *_bin2hex(byte *bin, size_t len) {
|
||||
out[len*2] = '\0';
|
||||
return out;
|
||||
}
|
||||
|
||||
/* via stackoverflow. yes, lazy */
|
||||
size_t _hex2bin(const char *hex_str, unsigned char *byte_array, size_t byte_array_max) {
|
||||
size_t hex_str_len = strlen(hex_str);
|
||||
size_t i = 0, j = 0;
|
||||
|
||||
// The output array size is half the hex_str length (rounded up)
|
||||
size_t byte_array_size = (hex_str_len+1)/2;
|
||||
|
||||
if (byte_array_size > byte_array_max)
|
||||
{
|
||||
// Too big for the output array
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (hex_str_len % 2 == 1)
|
||||
{
|
||||
// hex_str is an odd length, so assume an implicit "0" prefix
|
||||
if (sscanf(&(hex_str[0]), "%1hhx", &(byte_array[0])) != 1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
i = j = 1;
|
||||
}
|
||||
|
||||
for (; i < hex_str_len; i+=2, j++)
|
||||
{
|
||||
if (sscanf(&(hex_str[i]), "%2hhx", &(byte_array[j])) != 1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return byte_array_size;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user