3

given the following string:

char *hexa_string= "ab010210000000000000000000000a0040890080006400950000920050050900300c3b811504a0ff2c8cf5e91740e466b219c2a3318ce80b66e1c43ba65ade66"

how can I convert it to buffer so that any cell in the buffer will be the value of any two digits from the above string ?
I tried to do something like that:

    char *hexa_string= "ab010210000000000000000000000a0040890080006400950000920050050900300c3b811504a0ff2c8cf5e91740e466b219c2a3318ce80b66e1c43ba65ade66";
    int hexa_string_size = strlen(hexa_string);
    uint8_t hexa_buffer[64];
    char *temp = hexa_string;
    for(int i = 0; i < hexa_string_size; i++){
        sscanf(temp, "%02hhx", &hexa_buffer[i]);
        temp += 2;
    }

but while I trying to printf the hexa_buffer I see nothing..
I want to get the following buffer:

hexa_buffer[0] = 0xab
hexa_buffer[1] = 0x01
hexa_buffer[2] = 0x02
..
  • 3
    You're close. `sscanf` is a good tool for this job. – Steve Summit Dec 18 '22 at 13:30
  • 2
    You're reading too far into `hexa_string`, and overflowing `hexa_buffer`. You want `for(int i = 0; i < hexa_string_size/2; i++)`. – Steve Summit Dec 18 '22 at 13:36
  • 1
    Note that you won't be able to "print" `hexa_buffer` anyway, since it's not really a string. In particular, `printf("%s", hexa_buffer)` is only going to print the first four bytes, because then it hits a `00`, which is a null terminator. – Steve Summit Dec 18 '22 at 13:38
  • 1
    You didn't show the code where you attempt to print `hexa_string`. That's probably where your (biggest) mistake is... – Support Ukraine Dec 18 '22 at 13:43
  • `for (int i = 0; i < hexa_string_size / 2; ++i) { printf("%02d\n", hexa_buffer[i]); }` – Zakk Dec 18 '22 at 14:12

0 Answers0