How to typecast from integer to string and vice versa?

Login to reply to this topic.
Sun, 2005-04-10 17:51
Joined: 2005-04-10
Forum posts: 19
Hi!

I´m very new to Symbian and I need to know how to typecast variables from integers to strings and vice versa?

For example:

I have a TInt variable and I need to typecast this variable to TDesC. Can I do that? Are there other options? Can I use the itoa and atoi functions from C-language?

Sun, 2005-04-10 18:07
Joined: 2004-10-27
Forum posts: 169
How to typecast from integer to string and vice versa?
+++++++++++++ String To Int+++++++
// TDesC& aStr  ...
...
 TInt aValue;
 TLex(aStr).Val(aValue);
 return aValue;

+++++++++++++++++ and Vice Versa:

// aIndex is an Integer...
   HBufC8 *iMyBufNumber = HBufC8::NewLC(255);
   TPtr8 bufNumberPtr(iMyBufNumber->Des());
   bufNumberPtr.AppendNum(aIndex);

I hope it helps
Sun, 2005-04-10 18:12
Joined: 2005-04-10
Forum posts: 19
How to typecast from integer to string and vice versa?
Quote from: marycore
+++++++++++++ String To Int+++++++
// TDesC& aStr  ...
...
  TInt aValue;
  TLex(aStr).Val(aValue);
  return aValue;

+++++++++++++++++ and Vice Versa:

// aIndex is an Integer...
    HBufC8 *iMyBufNumber = HBufC8::NewLC(255);
    TPtr8 bufNumberPtr(iMyBufNumber->Des());
    bufNumberPtr.AppendNum(aIndex);

I hope it helps

Thanks! I haven´t tried yet, because I want to ask this question first.

Do i have to reallocate the memory used by the iMyBufNumber-variable after the typecast? If I have to how do I do that?

And other question too: why is 255 send as a parameter to NewLC?
Sun, 2005-04-10 18:21
Joined: 2005-04-10
Forum posts: 19
How to typecast from integer to string and vice versa?
My application collapsed after trying this:

Code:

TKeyResponse CTestAppAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
   {
       HBufC8 *iMyBufNumber = HBufC8::NewLC(255);
       TPtr8 bufNumberPtr(iMyBufNumber->Des());
       bufNumberPtr.AppendNum(aKeyEvent.iScanCode);
   }


What went wrong?
Mon, 2005-04-11 04:30
Joined: 2005-02-07
Forum posts: 13
Integer to String
try using this to convert your Integer to String.

//Integer to String;
TBuf <20> yourString;
TInt yourInteger;

yourString.Num(yourInteger);
Mon, 2005-04-11 05:14
Joined: 2003-04-03
Forum posts: 39
How to typecast from integer to string and vice versa?
Quote from: PeteH
My application collapsed after trying this:

Code:

TKeyResponse CTestAppAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
   {
       HBufC8 *iMyBufNumber = HBufC8::NewLC(255);
       TPtr8 bufNumberPtr(iMyBufNumber->Des());
       bufNumberPtr.AppendNum(aKeyEvent.iScanCode);
   }


What went wrong?

When you call "LC" function you should in the same function call CleanupStack::Pop (or PopAndDestroy).

Actualy bofore asking better to write what exact error occurs Wink
  • Login to reply to this topic.