How to convert string to GUID?
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?
Ben
I haven''t tested this source code, but it should work...
here is your guid string:
"{098f2470-bae0-11cd-b579-08002b30bfeb}"
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;
}
adamm@san.rr.com
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
Thanks..
adamm@san.rr.com
adamm@san.rr.com
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 = (BYTE)HexToLong(str, j, j+2);<br> return ret;<br>}<br></tt> </i>
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 = (BYTE)HexToLong(str, j, j+2);<br> return ret;<br>}<br></tt> </i>
adamm@san.rr.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement