Convert hex string to struct

R

Ross

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
 
S

Suman

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
 
M

Martin Ambuhl

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
 
S

Suman

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.

Brevity in description can't help you get much help.Please be specific
e.g.
if `word4' holds the MS/LS part of the string.

You might try out the following:
*) copy the string to a local buffer
*) start with a pointer 8 chars before the end of the string,
*) call atoi()/atol()/strtol() or somesuch and store it to your
favoured
member, i.e. word4/word1
*) put a NULL character at the point where you started
*) repeat the above three steps, till you have nothing on your plate.

Of course, there are issues that need to be worked out. But till you
give us a better explanation.

HTH,
Suman.
 
A

Al Bowers

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;
};

One potential solution is to use function strtoul which
can give you some validation of the conversion.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef unsigned long ulong32_t;

struct uniqueid
{
ulong32_t word1;
ulong32_t word2;
ulong32_t word3;
ulong32_t word4;
};

int Convert(struct uniqueid *p, const char *s);

int main(void)
{
struct uniqueid test = {0};
char *id = "0009dbaa00004c00000000fb82ca621c";

if(Convert(&test, id))
{
printf("For id \"%s\"\n",id);
printf("test.word1 = %lu\n"
"test.word2 = %lu\n"
"test.word3 = %lu\n"
"test.word4 = %lu\n",
test.word1,test.word2,test.word2,
test.word4);
}
return 0;
}

int Convert(struct uniqueid *p, const char *s)
{
char buf[9] , *endp;
const char *cs;
int i;

if(strlen(s) != 32) return 0;
for(i = 0,cs = s; i < 4;i++,cs+=8)
{
sprintf(buf,"%.8s",cs);
switch(i)
{
case 0: p->word1 = strtoul(buf,&endp,16);
break;
case 1: p->word2 = strtoul(buf,&endp,16);
break;
case 2: p->word3 = strtoul(buf,&endp,16);
break;
case 3: p->word4 = strtoul(buf,&endp,16);
break;
}
if(*endp != '\0') return 0;
}
return 1;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,631
Members
45,338
Latest member
41Pearline46

Latest Threads

Top