I'm trying to create a function that converts a hex string into an array of hex bytes. Example: str = "1c01"
-> hex_bytes = { 0x1c, 0x01 }
.
When I try to print the hex values all I get are 0s. I'm thinking it's something to do with my pointers but I am not sure. Any help would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *input_1 = "1c0111001f010100061a024b53535009181c";
unsigned int *str_to_hexbytes(const char *hex_str) {
size_t len = strlen(hex_str);
unsigned int *hex = malloc(sizeof(unsigned int)* len / 2);
for(int i, j = 0; i < len; i += 2, j++) {
char tmp[2];
strncpy(tmp, hex_str + i, 2);
hex[j] = strtol(tmp, NULL, 16);
}
return hex;
}
int main(void) {
size_t len = strlen(input_1) / 2;
unsigned int *hex = str_to_hexbytes(input_1);
for (int i = 0; i < len; i++) {
printf("%x ", hex[i]);
}
return 0;
}