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
.
Have you tried strtol
?
int val = (int) strtol (hex_string, NULL, 16);
Repeat on substrings if it is a flattened array.
Regards
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.