2

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;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    You can remove the `c++` tag since your code is pure C if I'm not wrong. – digito_evo Dec 11 '21 at 16:04
  • 1
    `"I'm thinking it's something to do with my pointers but I am not sure."` -- Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Dec 11 '21 at 16:10

2 Answers2

4

tmp only has enough space to store the two characters you copy in. It does not have space for a null byte to terminate the string, and in fact strncpy won't write that null byte since it didn't find one in the two characters it read.

As a result, the strtol function reads past the end of the array, triggering undefined behavior.

Make tmp 3 characters long and add the null byte manually.

Also, you're only initializing j, not i, so make sure you do that as well.

for(int i = 0, j = 0; i < len; i += 2, j++) { 
    char tmp[3];
    strncpy(tmp, hex_str+i, 2);
    tmp[2]=0;
    hex[j] = strtol(tmp, NULL, 16);
}
dbush
  • 205,898
  • 23
  • 218
  • 273
1

You are not initializing i with 0. That might be your problem. int i, j = 0; only changes j's value to zero, i remains garbage since it is allocated from stack.

Also a few suggestions:

  • Since you are using string's length in main too, you can only calculate it in main and send it to the function.
  • You used a 'malloc' which requires you to call 'free' also. After you are done using the pointer call free(hex)
  • Iterate until len - 1 since you are using one memory block ahead in your for loop's body.
Larcis
  • 134
  • 7
  • `"which requires you to call 'free' also"` -- I disagree with the word "requires". A formulation like "it is generally recommended" may be more appropriate. [What REALLY happens when you don't free after malloc?](https://stackoverflow.com/q/654754/12149471) – Andreas Wenzel Dec 11 '21 at 16:34
  • I agree, bad word choice, English is my second language. – Larcis Dec 11 '21 at 16:36