将CString字符串输入转化成整数的实现方法

如下所示:

BOOL IsHexFormat(LPCTSTR pStr)

{

if (pStr[0] == L'0' && ((pStr[1] == L'x') || (pStr[1] == L'X'))){

return TRUE;

}

return FALSE;

}

BOOL IsInputValid(LPCTSTR pStr)

{

int i;

BOOL res;

BOOL IsHex;

i = 0;

res = TRUE;

IsHex = IsHexFormat(pStr);

while (pStr[i] != L'\0'){

if (pStr[i] >= L'0' && pStr[i] <= L'9'){

i++;

continue;

}

else if (IsHex && (i == 1)){

i++;

continue;

}

else if (IsHex &&

((pStr[i] >= L'a' && pStr[i] <= L'f') ||

(pStr[i] >= L'A' && pStr[i] <= L'F') )) {

i++;

continue;

}

else{

res = FALSE;

break;

}

}

return res;

}

UINT32 CStrHex2Uint32(LPCTSTR pStr)

{

int i = 0;

UINT32 res = 0;

while (pStr[i] != L'\0'){

if (pStr[i] >= L'0' && pStr[i] <= L'9'){

res = res * 16 + pStr[i] - L'0';

}

else if (pStr[i] >= L'a' && pStr[i] <= L'f'){

res = res * 16 + pStr[i] - L'a' + 10;

}

else if (pStr[i] >= L'A' && pStr[i] <= L'F'){

res = res * 16 + pStr[i] - L'A' + 10;

}

else{

break;

}

i++;

}

return res;

}

/* 将CString转化成UINT32, 0x开头的识别成十六进制,其它为十进制*/

BOOL CStr2Uint32(CString str, UINT32 *pData)

{

LPCTSTR pStr;

pStr = (LPCTSTR)str;

if (!IsInputValid(pStr)){

*pData = 0;

return FALSE;

}

if (IsHexFormat(pStr)){

UINT32 Data;

pStr = &pStr[2];

*pData = CStrHex2Uint32(pStr);

}

else{

*pData = _wtoi((wchar_t *)pStr);

}

return TRUE;

}

以上是 将CString字符串输入转化成整数的实现方法 的全部内容, 来源链接: utcz.com/z/344939.html

回到顶部