• Advertisement

Archived

This topic is now archived and is closed to further replies.

How to convert string to GUID?

This topic is 6244 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have load a GUID value from the registry which is in string format. Now I want convert it to GUID format, how to do it?

Share this post


Link to post
Share on other sites
Advertisement
I haven''t tested this source code, but it should work...

here is your guid string:
"{098f2470-bae0-11cd-b579-08002b30bfeb}"


long HexToLong(const char *str, int start, int end)
{ static char *hexs = "0123456789abcdef";
long total=0;
for(; start<=end; start++)
total = total*16 + hexs[tolower(str[start])]-''0'';
return total;
}

GUID StrToGUID(const char *str)
{ GUID ret;
int i, j=19;
ret.Data1 = HexToLong(str, 1, 8);
ret.Data2 = (WORD)HexToLong(str, 10, 13);
ret.Data3 = (WORD)HexToLong(str, 15, 18);
for(i=0; i<8; j+=2,i++)
ret.Data4 = (BYTE)HexToLong(str, j, j+2);
return ret;
}

Share this post


Link to post
Share on other sites
Hmm.. that didnt look very good. Can you email me and tell me how to post code, if you know?

Thanks..
adamm@san.rr.com

Share this post


Link to post
Share on other sites
and oops! i made a mistake.. sorry! (that''s what i get for not even double checking it..)

this should be better..


GUID StrToGUID(const char *str)
{ GUID ret;
int i, j=20;
ret.Data1 = HexToLong(str, 1, 8);
ret.Data2 = (WORD)HexToLong(str, 10, 13);
ret.Data3 = (WORD)HexToLong(str, 15, 18);
for(i=0; i<2; j+=2,i++)
ret.Data4 = (BYTE)HexToLong(str, j, j+2);
for(j++; i<8; j+=2,i++)
ret.Data4[i] = (BYTE)HexToLong(str, j, j+2);
return ret;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
UuidFromString is a lot simpler...

-Mike

Share this post


Link to post
Share on other sites

  • Advertisement