CONVERT_STR_2_GUID 方法一:
#define CONVERT_STR_2_GUID(cstr, stGuid) do\
{\
swscanf_s((const wchar_t*)cstr, L"{%8x-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x}",\
&(stGuid.Data1),&(stGuid.Data2),&(stGuid.Data3),\
&(stGuid.Data4[0]),&(stGuid.Data4[1]),&(stGuid.Data4[2]),&(stGuid.Data4[3]),\
&(stGuid.Data4[4]),&(stGuid.Data4[5]),&(stGuid.Data4[6]),&(stGuid.Data4[7]));\
}while(0);
方法二(新):1.在{}带空格容错处理上,2.在非法字符退出,有更好表现,不引起内容读取非法和字符非法上的出错
inline int CONVERT_STR_2_GUID(const unsigned char*cstr, GUID *pGuid)
{
if (pGuid == NULL || cstr == NULL)return -1;
unsigned char *pTemp = (unsigned char *)strchr( (char *)cstr,'{');
if (pTemp == NULL )return -1;
unsigned char *pEnd = (unsigned char *)strchr((char *)pTemp, '}');
if (pTemp == NULL || pEnd == NULL|| pEnd-pTemp<=35)return -1;
unsigned char *pG = (unsigned char *)pGuid;
const int Index[16] = { /* unsigned long*/3,2,1,0,/*unsigned short*/5,4,/*unsigned short*/7,6,/*unsigned char[8]*/8,9,10,11,12,13,14,15 };
int nGet = 0;
while (pTemp[0] != '}'&& pTemp[1] != '}'&& pTemp[0] != '\0' && pTemp[1] != '\0')
{
if (pTemp[0] == '{' || pTemp[0] == '-' || pTemp[0] == ' ' || pTemp[0] == ',' || pTemp[0] == ':' || pTemp[0] == '}')
{
pTemp++;
continue;
}
BYTE val0, val1;
if (pTemp[0] >= '0' && pTemp[0] <= '9')
{
val0 = pTemp[0] - '0';
}
else if (pTemp[0] >= 'a' && pTemp[0] <= 'f')
{
val0 = pTemp[0] - 'a' + 10;
}
else if (pTemp[0] >= 'A' && pTemp[0] <= 'F')
{
val0 = pTemp[0] - 'A' + 10;
}
else
{
*pGuid = { 0 };
return -1;
}
if (pTemp[1] >= '0' && pTemp[1] <= '9')
{
val1 = pTemp[1] - '0';
}
else if (pTemp[1] >= 'a' && pTemp[1] <= 'f')
{
val1 = pTemp[1] - 'a' + 10;
}
else if (pTemp[1] >= 'A' && pTemp[1] <= 'F')
{
val1 = pTemp[1] - 'A' + 10;
}
else
{
*pGuid = { 0 };
return -1;
}
pG[Index[nGet]] = (val0 << 4) | val1;
nGet++;
if (nGet >= 16)break;
pTemp++;
if (pEnd== pTemp ||pTemp[0] == '\0' || pTemp[0] == '}')break;
pTemp++;
}
if (nGet != 16)
{
*pGuid = { 0 };
return -1;
}
return 0;
}
inline int CONVERT_GUID_2_STR( GUID *pGuid, unsigned char*cstr)
{
if (pGuid == NULL || cstr == NULL)return -1;
//typedef unsigned long long int uint64;
sprintf((char*)cstr, "{%8X-%4X-%4X-%016I64x}",
pGuid->Data1, pGuid->Data2, pGuid->Data3, *(uint64*)pGuid->Data4);
return 0;
}
版权声明:本文为liu3zy原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。