×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Regular Contributor
    Join Date
    Apr 2005
    Posts
    162

    String conversion

    Hi Symbies,
    I just want to convert integer to string. It#s quite easy in other languages, but what's up with C++ for Symbian?
    It works with this code:

    TBuf16<256> pMyString;
    _LIT16(KmyString0, "%b %c %d");
    pMyString.Format(KmyString0, 65, 65, 65);

    iToDoLabel = new (ELeave) CEikLabel;
    iToDoLabel->SetContainerWindowL( *this );
    iToDoLabel->SetTextL( pmyString0 );

    But I want to have a dynamic buffer, for appending; because I don't know, how long the String becomes.
    So I tried that code:

    _LIT16(KmyString1, "%b %c %d");
    HBufC* helloHeap = KmyString1().AllocLC();
    TPtr16 pmyString1 (helloHeap->Des());
    pmyString1.Format(KmyString1, 65, 65, 65);

    iToDoLabel = new (ELeave) CEikLabel;
    iToDoLabel->SetContainerWindowL( *this );
    iToDoLabel->SetTextL( pmyString1 );

    I can compile, but it panics in my emulator.
    Why???

    Please help me.

    Best regards
    Christian

  2. #2
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,165
    what is the panic shown ?

    Anyway, I think your allocation is allocating too short buffer for the terget string, maybe you should add some lenght to it, so it could handle longer number strings, since KmyString, doesnt really know if one number takes one character, or more.

    yucca

  3. #3
    Regular Contributor
    Join Date
    Apr 2005
    Posts
    162
    Oh hi SymbianYucca,
    quite nice, to have you in this board...
    It just crashes. No message.
    I don't understand what you meant.
    Shall I:
    HBufC* exampleHeap = HBufC::NewLC(KmyString0().Length())

    _LIT16(KmyString1, "%b %c %d");
    HBufC* helloHeap = HBufC::NewLC(KmyString1().Length());
    helloHeap = KmyString1().AllocLC();
    TPtr16 pmyString1 (helloHeap->Des());
    pmyString1.Format(KmyString1, 65, 65, 65);

    iToDoLabel = new (ELeave) CEikLabel;
    iToDoLabel->SetContainerWindowL( *this );
    iToDoLabel->SetTextL( pmyString1 );

    That crashes too.
    How do you convert an integer into a String for putting it onto a Label? I think this can't be such difficult.

    Best regards
    Chris

  4. #4
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,165
    what I mean, is that if you say like this: KmyString1().AllocLC();, the KmyString1 lenght can not really be thought to be enough long to accomondate longest integers. Propable the length reserved for numbers is zero or one, and you could have integer that is a lot longer in characters. So just reserve longer buffer for the string.

    You could also use function provided by the class to add the data, for examlpe helloHeap->Des().Num(MyInt) and helloHeap->Des().AppendNum(MyInt) could be used to convert numbers into the strings.

    yucca

  5. #5
    Regular Contributor
    Join Date
    Apr 2005
    Posts
    162
    Hi Yucca,
    your hints were quite helpful.
    I made it like this:

    _LIT(KText1,"%d!");
    HBufC* bufText1 = HBufC::NewL(KText1().Length());
    *bufText1 = KText1;
    TPtr pString(bufText1->Des());
    pString.AppendNum(i);

    I understand it like this:
    A buffer on the heap is created with the length of the literal, which has to be put on it (the first three lines).
    Then a pointer is created, which gets the adress of the buffer (with buffer->Des()).
    With that pointer you can work quite comfortable (Copy(), Format() or Delete()).
    When you want to add another string, which might be longer, then increase the buffer with:
    bufText1 = bufText1->ReAllocL(KText1().Length() + KText2().Length());
    After that the same procedure as mentioned before.
    I can advise the method AppendNum(). It's quite convenient for working.
    But please note: Always handle your stack with
    CleanupStack::PushL(bufText1);
    CleanupStack::PopAndDestroy(bufText1);

    Otherwise your system will crash.
    So that's all for the moment.
    If somebody would have a better way for stringhandling, he can append his advantage.

    Best regards
    Christian

  6. #6
    Regular Contributor
    Join Date
    Apr 2005
    Posts
    274

    Integer to String

    TInt num1 = 30;
    TBuf<10> buf1;
    buf1.Num(num1);
    I think this could be your solution.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×