现在的位置: 首页 > 综合 > 正文

【VC】CString类型数据(十六进制) TO int类型

2014年02月17日 ⁄ 综合 ⁄ 共 448字 ⁄ 字号 评论关闭

 

当我们获取数据时,是CString类时。。

1.CString 数据是 1234,4567等

int nNum = wtoi(CString);  这样是可以直接转换的。。

 

2。CString 数据是 FFFF,FF33 等

这样就得自己写函数来实现了。。

 

BOOL StrHexToDec(CString szStr,int& nData)
{
	int nTemp = 0;
	nData = 0;
	int nLen = szStr.GetLength();
	for (int i = 0;i<nLen;i++)
	{
		TCHAR c = szStr.GetAt(i);
		if (c >= '0' && c <= '9')
		{
			nTemp = c - '0';
		}
		else if (c >= 'a' && c <= 'f')
		{
			nTemp = c - 'a'+10;
		}
		else if (c >= 'A' && c <= 'F')
		{
			nTemp = c - 'A'+10;
		}
		else
		{
			return FALSE;
		}
		nTemp <<= ((nLen-i-1)<<2);   
		nData |= nTemp;
	}	
	return TRUE;
}

IDE:2008+SP1  Unicode 编码

抱歉!评论已关闭.