# 转换十六进制字符串为整数

Den123 在 10 Aug 2007 创建

`//十六进制字符串转为整数// "1234" => 0x1234static TInt 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;	}`

`TInt res = StringUtil::HexString2Int(_L("Ef56"));`

SDK提供的TLex类可以实现把16进制字符串转换成整数:

`_LIT( KTestHexString, "Ef56" ); TLex lex( KTestHexString ); TUint num(0); lex.Val( num, EHex ); 另外TRadix还可以进行二进制,八进制的转换: EBinary Convert number into binary character representation.  EOctal Convert number into octal character representation.  EDecimal Convert number into decimal character representation.  EHex Convert number into hexadecimal character representation`