Numbers to/from strings
Article Metadata
To get numbers out of strings, you can use class called TLex. It can be used for example like this:
_LIT(KtxRemoveUniHex ,”0x”);
TInt GetNumberL(const TDesC& aString)
{
TInt RetNumber(0);
if(aString.Left(2) == KtxRemoveUniHex
&& aString.Length() > 2)
{
TPtrC aString2(aString.Right(aString.Length() - 2));
TInt err;
TLex aLex;
aLex.Assign(aString2);
User::LeaveIfError(aLex.Val(RetNumber, EHex));
}
else
{
TLex MyLex(aString);
User::LeaveIfError(MyLex.Val(RetNumber));
}
}
As shown in the code with TLex you can also define which numbering system is supposed to be used when converting strings to numbers. Supported numbering systems are:
- EBinary : Binary numbers
- EOctal: Octal numbers
- EDecimal : Decimal numbers
- EHex : Hexadecimal numbers
If you want to put numbers into strings you can use the functions provided by the TDes-class, i.e. Num & AppendNum,:
TInt Numberr(138);
TBuf<200> Buffer;
Buffer.Num(Numberr,EHex)
Buffer.AppendNum(Numberr)
As with TLex you can also define the numbering system used when converting numbers to strings.


(no comments yet)