Search code examples
cstringhexstrtol

Split a string and convert individual pieces to hexadecimal


I have a string like "03FE" holding hexadecimal values. I need to split this string in two parts, and convert each individual part to the equivalent hexadecimal.

That is, I need 0x03 in one variable and 0xFE in another variable.

For example, if I didn't have to split the string, this is how I would have done it:

 char *p;
 uint32_t uv=0;
 uv=strtoul(&string_to_convert, &p, 16);

How shall I proceed if I needed to split the string?


Solution

  • Split the output of strtoul instead:

    uint8_t uv_hi = uv >> 8;
    uint8_t uv_lo = uv & 0xFF;