Ross said:
Anyone have some code to help me understand how I can convert a given
hex string, say "0009dbaa00004c00000000fb82ca621c," into a struct of
this form:
struct uniqueid {
ulong32_t word1;
ulong32_t word2;
ulong32_t word3;
ulong32_t word4;
};
Thanks very much.
-Ross
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct uniqueid
{
unsigned long word1;
unsigned long word2;
unsigned long word3;
unsigned long word4;
};
int main(void)
{
char input[] = "0009dbaa00004c00000000fb82ca621c";
struct uniqueid x;
if (strlen(input) != 32) {
fprintf(stderr, "The string \"%s\" is not 32 characters long.\n"
"Bailing ... ", input);
exit(EXIT_FAILURE);
}
if (4 !=
sscanf(input, "%8lx%8lx%8lx%8lx", &x.word1, &x.word2, &x.word3,
&x.word4)) {
fprintf(stderr,
"The string \"%s\" did not lead to 4 conversions.\n"
"Bailing ... ", input);
exit(EXIT_FAILURE);
}
printf
("It is possible that the string \"%s\"\n"
"was not parsed correctly.\n"
"To be sure of a correct conversion, use more code\n"
"more sophisticated than this simple scanf conversion.\n\n"
"In any case, it was parsed as the unsigned longs\n"
" %#0lx %#0lx %#0lx %0lx\n", input, x.word1, x.word2, x.word3,
x.word4);
return 0;
}
It is possible that the string "0009dbaa00004c00000000fb82ca621c"
was not parsed correctly.
To be sure of a correct conversion, use more code
more sophisticated than this simple scanf conversion.
In any case, it was parsed as the unsigned longs
0x9dbaa 0x4c00 0xfb 82ca621c