Namespaces
Variants
Actions
Revision as of 09:18, 25 July 2012 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Numbers to/from strings

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.

76 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved