I have a variable length string where each character represents a hex digit. I could iterate through the characters and use a case statement to convert it to hex but I feel like there has to be a standard library function that will handle this. Is there any such thing?
Example of what I want to do. "17bf59c" -> int intarray[7] = { 1, 7, 0xb, 0xf, 5, 9, 0xc}
No, there’s no such function, probably because (and now I’m guessing, I’m not a C standard library architect by a long stretch) it’s something that’s quite easy to put together from existing functions. Here’s one way of doing it decently:
Note: the above is untested.
Also note things that maybe aren’t obvious, but still subtly important (in my opinion):
constfor pointer arguments that are treated as “read only” by the function.outis pointing at, usesizeof *out.malloc()in C.malloc()succeeded before using the memory.