下面的例子展示了如何将8位以内的16进制字符串转换为整数, 我相信这对于很多人都是很有用的,尤其是对用sdk开发的windows程序员,下面的代码可以原封不动的拷贝使用。虽然代码很简单,但是自己写起来还得费一点小心思,
// eg.: lpszHexString = "600107D0"
DWORD HexString2DWORD(LPCTSTR lpszHexString)
{
int len = lstrlen(lpszHexString);
if ( len > 8 )
return 0;
DWORD res = 0;
DWORD tmp = 0;
for ( int i = 0; i < len; i++ )
{
if ( lpszHexString[i] >= '0' && lpszHexString[i] <= '9' )
tmp = lpszHexString[i] - '0';
else if ( lpszHexString[i] >= 'a' && lpszHexString[i] <= 'f' )
tmp = lpszHexString[i] - 'a' + 10;
else if ( lpszHexString[i] >= 'A' && lpszHexString[i] <= 'F' )
tmp = lpszHexString[i] - 'A' + 10;
else
tmp = 0;
tmp <<= ((len - i - 1) << 2);
res |= tmp;
}
return res;
}
|
author: thinker
e-mail: cnhnyu<AT>gmail.com
qq: 94483026