Symbian 十六进制字符串转换成十进制数
2010-03-05 05:47:00 来源:WEB开发网核心提示:TBuf<10> va;va.Append(_L("00Fa"));//十六进制字符串TInt result = HexString2Int(va);//转换成十进制数TInt CSocketsEngine::HexString2Int(const TDesC & aHexStr){TI
TBuf<10> va;
va.Append(_L("00Fa"));//十六进制字符串
TInt result = HexString2Int(va);//转换成十进制数
TInt CSocketsEngine::HexString2Int(const TDesC & aHexStr)
{
TInt len = aHexStr.Length();
//防止溢出
if(len > 8) return 0;
TInt res = 0;
TInt tmp = 0;
const TUint16 * hexString = aHexStr.Ptr();
for (TInt i = 0; i < len; i++)
{
if( hexString[i]>= '0'&& hexString[i]<= '9' )
{
tmp = hexString[i]-'0';
} else if(hexString[i]>= 'a'&& hexString[i] <= 'f' )
{
tmp = hexString[i] -'a'+10;
}else if( hexString[i]>='A'&& hexString[i]<='F' )
{
tmp = hexString[i] - 'A' +10;
} else {
res = 0;
}
tmp <<= ((len-i-1)<<2);
res |= tmp;
}
return res;
}
赞助商链接