Reputation: 117
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?
Upvotes: 1
Views: 297
Reputation: 363487
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
.
Upvotes: 4
Reputation: 1544
Have you tried strtol
?
int val = (int) strtol (hex_string, NULL, 16);
Repeat on substrings if it is a flattened array.
Regards
Upvotes: 0