×
Namespaces

Variants
Actions

Numbers to/from strings

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: symbianyucca (09 Apr 2007)
Last edited: hamishwillee (25 Jul 2012)

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.

This page was last modified on 25 July 2012, at 09:18.
21 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×