-1

I've searched Stackoverflow, and there are many answers - but not to the exact problem that I'm seeing (using gcc on macOS, if that makes a difference - Mac specific suggestions won't do though because I need this to be cross platform). I know how to convert a string to Hex into a buffer - or, at least, I thought I did. I do it like this:

unsigned char* buffer_from_hexstring(char* string) {
    unsigned char* HexBuffer = (unsigned char *)malloc( (strlen(string) / 2) * sizeof(unsigned char) );

    for (size_t count = 0; count < strlen(string); count++) {
        sscanf(string, "%2hhx", &HexBuffer[count]);
        string += 2;
    }

    return HexBuffer;
}

I call my function as follows:

int main(int argc, char *argv[]) {
    char* hexstring = "beefcafebeefcafebeefcafe";

    unsigned char* buffer = buffer_from_hexstring(hexstring);

    printf("Result: ");
    for(size_t count = 0; count < (sizeof(buffer) * sizeof(*buffer)); count++) {
        printf("%02x", buffer[count]);
    }

    free(buffer);
}

And the result that I get is beefcafebeefcafe. I'm missing a beefcafe. In fact, whatever I do I only get 16bytes back - which is no useful if I want to convert more bytes.

I'm sure that it's an obvious error - but I can't see it. Can you?

headbanger
  • 1,038
  • 1
  • 11
  • 32
  • 2
    `sizeof(buffer)` doesn't give you the size of your buffer. – tkausl Nov 07 '19 at 09:23
  • ...it gives you the size of `buffer`, meaning the size of a pointer. You'll need to return both the buffer and its size. – ikegami Nov 07 '19 at 09:23
  • 1
    You seem to have some misunderstanding about digits and bytes... You print 16 hexadecimal *digits* which is 8 *bytes*. – Some programmer dude Nov 07 '19 at 09:29
  • 1
    @some programmer dude - Damn. Quite right. I do understand the difference, but I can't see wood for the trees at the moment! – headbanger Nov 07 '19 at 10:02
  • @tkausl, @ikegami - it's not perfect, but (in this case) it's correct. A char is one byte so, whilst I agree that `count < sizeof(buffer)` isn't perfect it is the same as `count < (sizeof(buffer) * sizeof(*buffer))` in this instance. But, since you insist, I'll amend the code. Doesn't fix the problem though. – headbanger Nov 07 '19 at 10:14

1 Answers1

0

One problem is the loop

for (size_t count = 0; count < strlen(string); count++)

iterates over all characters in the string string, but inside the loop you "extract" two characters at a time.

One simple solution is to iterate over half the length, since each hexadecimal number you extract is two digits:

for (size_t count = 0; count < strlen(string) / 2; count++)

You also make the same mistake when allocating memory for the HexBuffer, you allocate twice as much as needed.

You also need to take this into account when you print the values using the returned pointer.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    These are definitely bugs that need fixing, but this doesn't answer the question. See the comments on the question. – ikegami Nov 07 '19 at 09:27
  • Its a good point - but it doesn't solve the problem. In fact, I think a while loop is a better solution `while (strlen(string) > 0)` - but that doesn't solve the problem either. – headbanger Nov 07 '19 at 10:24