×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Regular Contributor
    Join Date
    Mar 2006
    Location
    Bangalore
    Posts
    148

    Unhappy Convert TBuf to Int

    Hii all

    I have a unique situation its as follows

    I want to convert a 1 byte TBuf descriptor to an integer but havent been sucessful the code looks like this...

    TBuf<1> iMetaLengthBuf ;
    iMetaLengthBuf.Copy("\x04") ; // This is a Hexadecimal value which i get
    // from a stream over a socket
    TInt iMetaLengthInt ;

    Now wat i want is to have 0x04 in iMetaLengthInt

    These are the approaches i have tried and failed.

    1) Using TLex

    TLex16 temp(iMetaLengthBuf) ;
    temp.Val(iMetaLengthInt) ;

    Is there any other approach to do this. Please help...

    Thanks and Regards,
    Pavan

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,697

    Re: Convert TBuf to Int

    Note that \x04 is not a hexadecimal value. It is a character with ASCII code 4. If you want to extract 4 from the descriptor, try "0x04" (I am not sure if that actually works) or "04" and Val(iMetaLengthInt,EHex). For getting 4 from the \x04 use iMetaLengthBuf[0].

  3. #3
    Regular Contributor
    Join Date
    Mar 2006
    Location
    Bangalore
    Posts
    148

    Unhappy Re: Convert TBuf to Int

    HI wizard_hu

    I guess it is a Hex value coz i tried the following expt..

    TInt temp = 0x04;
    TBuf<1> temp1;
    temp1.Num(temp,EHex);

    When i debugged and saw what was stored in the desc temp1 it was
    temp1="\x04"

    And i also get values such as "\x0F" "\xFF" etc sometimes... and the expected value is hex.. so i thought it must be hex value...

    Regards,
    Pavan

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,697

    Re: Convert TBuf to Int

    TLit is for parsing text. If a descriptor contains "Hello World 04", TLex can tokenize it using the spaces, and it can also get the numerical value of "04". If you right click in the debugger, you can usually toggle the view to decimal numbers, where you will see 4, 15, 255 in your mentioned cases.
    Please accept the fact that descriptors are byte (8-bit ones) or word (16-bit ones) arrays, and not magic. If you put 4 in the 0th element of a descriptor, you can get it with the [] operator.
    Have you got any idea about the special meaning of backslash ("\") character in C/C++/Java, etc. string specifications?

  5. #5
    Regular Contributor
    Join Date
    Mar 2006
    Location
    Bangalore
    Posts
    148

    Re: Convert TBuf to Int

    hmm well if thats the case then could you please help me with how i can covert it to a numeric value.. as i am expecting a num val at that pos may be as u said it may be in ASCII string format...

    Thanks and Regards,
    Pavan

  6. #6
    Regular Contributor
    Join Date
    Mar 2006
    Location
    Bangalore
    Posts
    148

    Thumbs down Re: Convert TBuf to Int

    Hi wizard_hu

    Check this out pal...

    Numeric Escape Sequences

    You can also write numeric escape sequences using either octal or hexadecimal digits. An octal escape sequence takes one of the forms:

    \d or \dd or \ddd

    The escape sequence yields a code value that is the numeric value of the 1-, 2-, or 3-digit octal number following the backslash (\). Each d can be any digit in the range 0-7.

    A hexadecimal escape sequence takes one of the forms:

    \xh or \xhh or ...

    The escape sequence yields a code value that is the numeric value of the arbitrary-length hexadecimal number following the backslash (\). Each h can be any decimal digit 0-9, or any of the letters a-f or A-F. The letters represent the digit values 10-15, where either a or A has the value 10.

    Escape character syntax

    >>-\--+-escape_sequence_character-+----------------------------><
    +-x--hexadecimal_digits-----+
    '-octal_digits--------------'

    So... it is indeed a hex num...

    Regards,
    Pavan

  7. #7
    Regular Contributor
    Join Date
    Mar 2006
    Location
    Bangalore
    Posts
    148

    Re: Convert TBuf to Int

    Hi Wizard_Hu

    Thanks a lot pal... i got my issue solved ...

    I used the following

    TBuf<1> iMetaLengthBuf ;
    iMetaLengthBuf.Copy("\x04") ; // This is a Hexadecimal value which i get
    // from a stream over a socket
    TInt iMetaLengthInt = iMetaLengthBuf.Operator[](0);

    and in iMetaLengthInt i got 4

    Thanks and Regards,
    Pavan

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    Re: Convert TBuf to Int

    Quote Originally Posted by pavanmv View Post
    Hi Wizard_Hu

    Thanks a lot pal... i got my issue solved ...

    I used the following

    TBuf<1> iMetaLengthBuf ;
    iMetaLengthBuf.Copy("\x04") ; // This is a Hexadecimal value which i get
    // from a stream over a socket
    TInt iMetaLengthInt = iMetaLengthBuf.Operator[](0);

    and in iMetaLengthInt i got 4

    Thanks and Regards,
    Pavan

    Hi,
    Could you pls help me to convert a buffer which contains a IP address , how can i copy it to int with ".".

  9. #9
    Super Contributor
    Join Date
    Oct 2007
    Location
    જુનાગઢ - India
    Posts
    3,034

    Re: Convert TBuf to Int

    Quote Originally Posted by tejashree_das View Post
    Hi,
    Could you pls help me to convert a buffer which contains a IP address , how can i copy it to int with ".".
    You can not store any value in TInt with ".".

    To store each int value of IP address to TInt variables, use Locate() method of descriptor.
    Last edited by savaj; 2009-05-12 at 15:02.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Location
    Mumbai-India
    Posts
    379

    Re: Convert TBuf to Int

    Quote Originally Posted by tejashree_das View Post
    Hi,
    Could you pls help me to convert a buffer which contains a IP address , how can i copy it to int with ".".
    u can use TextUtils to separate out the numbers separated by '.' and take into descriptor

    then use descriptor to int conveter like as
    eg.
    valdes is descriptor taht is converted to integer

    TLex lexval(valdes);
    TInt val= 0;
    lexval.Val(val);

    val will contain the integer which is converted from valdes descriptor

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

    Re: Convert TBuf to Int

    Depending on what your goal with parsing is, you might find TInetAddr::Input really useful.

  12. #12
    Super Contributor
    Join Date
    Mar 2004
    Location
    Bangalore,India
    Posts
    2,146

    Re: Convert TBuf to Int

    Quote Originally Posted by tejashree_das View Post
    Hi,
    Could you pls help me to convert a buffer which contains a IP address , how can i copy it to int with ".".
    Wow you sure have opened an old thread, which I doubt the original poster would be even looking it. But anyways you could try something like this :-

    Code:
    TInt length = yourIPContainingBuffer.Length();
    RArray<TInt> yourDottedArray;
    TInt startPos = 0;
    TInt endPos = 0;
    
    for(TInt index = 0; index<length+1; index++)
      {
      if(endPos!=length && TChar(yourIPContainingBuffer[index])!=TChar('.'))
         {
         endPos++;
         }
      else
         {
         // You should copy everything starting from startPos to endPos into yourDottedArray
         TInt temp = 0;
         TInt tempLength = endPos-startPos;
         TBuf<4> tempBuf;
         tempBuf = yourIPContainingBuffer.Mid(startPos, tempLength);
         TLex tempLex(tempBuf);
         if(tempLex.Val(temp) == KErrNone)
        	 {
        	 // We got our first val, lets put it on the yourDottedArray
        	 yourDottedArray.AppendL(temp);
        	 }
         endPos++; //To accomodate the dot we got
         startPos = endPos;
         }
      }
    Cheers,
    Mayank

Similar Threads

  1. Why Are Pre-processors Inherently Evil?
    By grahamhughes in forum Mobile Java General
    Replies: 15
    Last Post: 2009-02-12, 08:42
  2. How to convert TBuf to Int?
    By yinjialiang in forum Symbian
    Replies: 1
    Last Post: 2003-09-28, 09:51
  3. Firmware bug or code bug displaying list on 3650
    By blackjack75 in forum Mobile Java General
    Replies: 2
    Last Post: 2003-09-24, 19:52
  4. some problems with label
    By vivienzhung in forum Mobile Java General
    Replies: 1
    Last Post: 2002-10-21, 09:52

Posting Permissions

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