468,544 Members | 1,765 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,544 developers. It's quick & easy.

Convert hex string to struct

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

Nov 15 '05 #1
4 5114


Ross wrote:
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


Nov 15 '05 #2
Ross wrote:
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
Nov 15 '05 #3


Ross wrote:
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.

Nov 15 '05 #4


Ross wrote:
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;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Nathan | last post: by
3 posts views Thread by news.hku.hk | last post: by
5 posts views Thread by sarab | last post: by
3 posts views Thread by Petr Jakes | last post: by
5 posts views Thread by XML newbie: Urgent pls help! | last post: by
3 posts views Thread by Ursula | last post: by
3 posts views Thread by Shawn Ferguson | last post: by
12 posts views Thread by Peter | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.