I have the following C string
"72e4247d3c91f424c62d909d7c1553a5"
It consists of 32 hex digits. It is an array with 4 integers in hex. How can I get back the numbers in form of integers from this array?
I have the following C string
"72e4247d3c91f424c62d909d7c1553a5"
It consists of 32 hex digits. It is an array with 4 integers in hex. How can I get back the numbers in form of integers from this array?
You'll have to parse the four 32-bit/8 hex digit chunks separately. The easiest way to do that is
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
void parse_hex4(char const *str, uint32_t num[4])
{
char buf[9]; // 8 hex digits + NUL
buf[8] = '\0';
for (int i=0; i<4; i++) {
memcpy(buf, str + i * 8, 8);
num[i] = strtoul(buf, NULL, 16);
}
}
This assumes str is zero-padded to be exactly 32 characters long and it does no input validation. If you're using the compiler from Redmond that is stuck in the 1980, use unsigned long instead of uint32_t.
buf and add a str+=8?strtoul's input is fixed and so is the endianness of num: both are little-endian. It's only inside the individual uint32_ts that endianness varies, but that's always true in C and it doesn't matter for the problem at hand.