Convert TBuf8 to TUint

Login to reply to this topic.
Fri, 2006-11-24 19:09
Joined: 2006-08-13
Forum posts: 3
I have a TBuf8 item and want to convert it to an integer (TUint) so I can count with it.

Any suggestions?

Fri, 2006-11-24 22:11
Joined: 2004-11-21
Forum posts: 33
Re: Convert TBuf8 to TUint
Quote from: bdekorte
I have a TBuf8 item and want to convert it to an integer (TUint) so I can count with it.

Any suggestions?
Try TLex8.

Regards, Bo

Fri, 2006-11-24 22:24
Joined: 2006-08-13
Forum posts: 3
Re: Convert TBuf8 to TUint
TLex8...

Can you give me an example?
Fri, 2006-11-24 22:39
Joined: 2004-11-21
Forum posts: 33
Re: Convert TBuf8 to TUint
Quote from: bdekorte
TLex8...

Can you give me an example?

Try something like this:

Code:
TUint GetMyNumberL( const TDesC8& aStringAsNumber)
{
    TLex8 lexer( aStringAsNumber);
    TUint value;
    User::LeaveIfError( lexer.Val( value, EDecimal));
    return value;
}

void CheckMyImplementationL()
{
    _LIT8( KAString, "1234abc");
    TBuf8<42> buffer( KAString);
    const TUint result = GetMyNumberL( buffer);
    _LIT( KPanicCategory, "GetMyNumberL");
    __ASSERT_ALWAYS( result == 1234, User::Panic( KPanicCategory, KErrGeneral));
}

Regards, Bo

Thu, 2007-01-18 13:17
Joined: 2003-10-10
Forum posts: 123
Re: Convert TBuf8 to TUint
has to be TLex (and its size vartients)
Sun, 2007-01-21 20:56
Joined: 2006-05-08
Forum posts: 152
Re: Convert TBuf8 to TUint
Mon, 2007-01-22 02:30
Joined: 2006-10-07
Forum posts: 131
Re: Convert TBuf8 to TUint
Max,

Num() converts integer to string, i.e. exactly the opposite of what the poster wanted...
Mon, 2007-01-22 12:51
Joined: 2006-12-12
Forum posts: 135
Re: Convert TBuf8 to TUint
## Convert TBuf to TInt ##

...

TBuf8    valTBuf;
TInt       valInt;

TLex valLex( valTBuf );
valLex.Val( valInt );

// than you will have the number that you have in the TBuf at the TInt

...

## Converte TInt to TBuf ##

...

TInt     valInt;
TBuf8  valBuf;

valInt = 123;

valBuf.AppendNum( valInt );

// than you will have the number that you have in the TInt at the TBuf

...

see ya
Smiley

Developer for PalmOS and Symbian.

by Serginho

  • Login to reply to this topic.