×

Discussion Board

Results 1 to 14 of 14
  1. #1
    Regular Contributor
    Join Date
    Aug 2008
    Posts
    70

    How to convert TPtrC8 to TInt?

    the number is TPtrC8 is 20 digits... Can anyone please tell me how to convert the number? this is the code:

    TInt GetNumber(const TDesC8& aData)
    {
    TLex8 lex(aData);
    TPtrC8 token;
    TBuf8 <512> buf;

    for(TInt i=0; i<4; i++)
    {
    token.Set(lex.NextToken());
    if(i==3)
    {
    buf.Append(token);
    buf.Delete(0,4);
    }
    }

    token.Set(buf.Alloc()->Des());

    TInt oldId;
    TLex8 temp(token);
    temp.SkipSpace();
    temp.Val(oldId);

    return oldId;
    }

    kindly help ...
    Last edited by Madz.R; 2008-08-23 at 11:17.

  2. #2
    Super Contributor
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    9,968

    Re: How to convert TPtrC8 to TInt?

    try using following code

    Code:
    TInt TDes2TIntL(TDesC &aDesC)
    {  
       TInt rstInt;
       TLex lexRec(aDesC);
       if(lexRec.Val(rstInt)!=KErrNone)
           User::Leave(0);
       return rstInt;
    }

  3. #3
    Regular Contributor
    Join Date
    Aug 2008
    Posts
    70

    Re: How to convert TPtrC8 to TInt?

    Thank you for replying...
    Some other value is getting returned... instead of the right one.

  4. #4
    Super Contributor
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    9,968

    Re: How to convert TPtrC8 to TInt?

    try above function, it works as a charm.

  5. #5
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,674

    Re: How to convert TPtrC8 to TInt?

    Quote Originally Posted by Madz.R View Post
    Thank you for replying...
    Some other value is getting returned... instead of the right one.
    What have you parsed, and what have you got?

  6. #6
    Regular Contributor
    Join Date
    Aug 2008
    Posts
    70

    Re: How to convert TPtrC8 to TInt?

    Hello,

    I was parsing a VCard and getting the UID... the uid is 20 digits. I guess TInt cannot hold such huge number... am I right?

  7. #7
    Regular Contributor
    Join Date
    Aug 2008
    Posts
    70

    Re: How to convert TPtrC8 to TInt?

    or can I make use of TUint instead of 64 digits? what is the range of TUint? please help...

  8. #8
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,674

    Re: How to convert TPtrC8 to TInt?

    I am not sure if you should really parse the UID field of a vCard, just search for vcard uid with Google.
    And a 20-digit decimal number does not fit into a 64-bit integer, even if it is unsigned: 20 / log 2 is 66.438... thus you would need a 67 bit number for that.

  9. #9
    Registered User
    Join Date
    Jul 2006
    Posts
    20

    Re: How to convert TPtrC8 to TInt?

    Quote Originally Posted by skumar_rao View Post
    try using following code

    Code:
    TInt TDes2TIntL(TDesC &aDesC)
    {  
       TInt rstInt;
       TLex lexRec(aDesC);
       if(lexRec.Val(rstInt)!=KErrNone)
           User::Leave(0);
       return rstInt;
    }
    You should never leave with error code 0, because that code means no errors, (http://www.symbian.com/developer/tec...rr.h.variables), and leaving means an error occurred. Leave with the return value of TLex::Val instead.

  10. #10
    Regular Contributor
    Join Date
    Aug 2008
    Posts
    70

    Smile Re: How to convert TPtrC8 to TInt?

    Thank you for the replies. I solved the issue. The 20 digit UID was not fitting in the TInt. Hence I used TInt64. Now the application works just fine...

    wizard_hu_, I am not parsing the UID but the VCard for UID.
    Thank you and others for your time, I appreciate.

  11. #11
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,674

    Re: How to convert TPtrC8 to TInt?

    Note that 18446744073709551615 is the largest number fitting into a 64-bit unsigned integer. So there are more 20-digit numbers which do not fit into 64 bits, than those which accidentally fit. You would still need 67bits for covering them all.

  12. #12
    Regular Contributor
    Join Date
    Aug 2008
    Posts
    70

    Re: How to convert TPtrC8 to TInt?

    The range of TInt64 is: 2^64 = +/-18446744073709551615.
    The UID that I was receiving was 15061974036633400012. So it has worked.. Then what data type should I use?

  13. #13
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,674

    Re: How to convert TPtrC8 to TInt?

    A simple descriptor, and not a number, as already suggested. You will never need to do mathematical operations with the UID, so you do not lose anything.

  14. #14
    Regular Contributor
    Join Date
    Aug 2008
    Posts
    70

    Smile Re: How to convert TPtrC8 to TInt?

    Ok, ya you are right. Thank you for your valuable reply.

Similar Threads

  1. Replies: 2
    Last Post: 2006-01-07, 01:39
  2. How to convert TPtrC8 to int, if TPtrC8 is a number
    By fcsheng in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2005-11-27, 15:10
  3. Replies: 6
    Last Post: 2005-10-03, 05:51
  4. How to convert a TText varible to TInt type?
    By liuajie in forum Symbian
    Replies: 1
    Last Post: 2002-07-30, 10:59

Posting Permissions

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