×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    73

    char array to TDesC

    Hi, since i want to draw a text on Window graphics context, i need to make use of something called strings (here descriptors)
    i think it will be a lot eaisier if i make another class/structure that will handle descriptors as strings, so i have few questions:


    How to initialize local variable TDesC var_name; should i use * or & between (since its a class it should be *.
    Since its a class it should be 'made' by new operator. well i didint see any of it anyway (in the symbian examples) i see only baseclassname::newLC or something like this, yes thats a pain for me ;]

    OK but going little further:


    I want to make a one local variable (the type is TDesC nothing else)
    then i want to insert there a text that is in the another variable that is char text[1024]; with int len; (decribes text length)

    i found something like this:



    Code:
    TDesC *AnsiStringToTBuff() {
    	
    	int i;
    	
    	HBufC* output = HBufC::NewLC(len);
    
    	for (i=0; i < len; i++) output->Des()[i] = text[i];	
    
    	
    		
    return output;
    	
    }
    and to check that it works i use:

    Code:
    TDesC * A;
    
    A = k.AnsiStringToTBuff();
    but wait i get USER 9 error and i even can't find what is it.


    so how should i convert it. I saw descriptor blog, and found nothing about stand alone variable TDesC * A;

    i want then to use
    gc.DrawText(A,TPoint(30,60)); but compiler throws an error that it have no match for * but for &

    but when i change TDesC * A; to & and get this code

    Code:
    TDesC & A; - error here
    gc.DrawText(A,TPoint(30,60));
    i get ERROR: const or & variable need initializer


    ;(

  2. #2
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,169

    Re: char array to TDesC

    You would need to have the buffer to which the TDesC is pointing to when you are creating it.

  3. #3
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,752

    Re: char array to TDesC

    Quote Originally Posted by melepeta View Post
    i think it will be a lot eaisier if i make another class/structure that will handle descriptors as strings, so i have few questions:
    Descriptors are rather versatile already, creating a new class for them will not really simplify the picture.
    How to initialize local variable TDesC var_name; should i use * or & between (since its a class it should be *.
    TDes/C should not be used as storage type, in practice they appear as type for method/function arguments and nothing else. Most of the time as TDes/C &, except if you want to allow "null", because then you use TDes/C *.
    Since its a class it should be 'made' by new operator. well i didint see any of it anyway (in the symbian examples) i see only baseclassname::newLC or something like this, yes thats a pain for me ;]
    "new", NewL, NewLC are all about creating an object on the heap. If you want that object on the heap being a descriptor, HBufC is the type for you.
    For the L and LC conventions you should familiarize yourself with the Cleanup Stack. In all Symbian SDK Help-s there is a section somewhere called "Essential idioms", it is not very long and it is rather important to read.
    I want to make a one local variable (the type is TDesC nothing else)
    then i want to insert there a text that is in the another variable that is char text[1024]; with int len; (decribes text length)
    A local variable can not have a sane reason for being TDesC (method arguments are not counted as local variables when I am saying that).
    Code:
    TDesC *AnsiStringToTBuff() {
        int i;
        HBufC* output = HBufC::NewLC(len);
        for (i=0; i < len; i++) output->Des()[i] = text[i];
        return output;
    }
    It should return HBufC (otherwise the type is lost and the object can not be deleted safely), and the method should be called AnsiStringToTBuffLC because it can leave (L) and it puts an object on the Cleanup Stack (C).
    Tiny trick: there are 8-bit desciptors too, and there is Copy between the two widths:
    Code:
    HBufC *AnsiToHBufLC(const char *aString)
    {
        TPtrC8 str(static_cast<const TUint8*>(aString));
        HBufC *ret=HBufC::NewLC(str.Length());
        ret->Des().Copy(str);
        return ret;
    }
    and to check that it works i use:

    Code:
    TDesC * A;
    
    A = k.AnsiStringToTBuff();
    but wait i get USER 9 error and i even can't find what is it.
    When you create a descriptor, its current length is 0 (the maximal length is the parameter, "len"). And the [] operator checks if you are pointing over the current length or not. HBufC has NewMax/L/C methods, or the TPtr provided by Des() has SetLength. So a ret->Des().SetLength(len) was missing.
    so how should i convert it. I saw descriptor blog, and found nothing about stand alone variable TDesC * A;

    i want then to use
    gc.DrawText(A,TPoint(30,60)); but compiler throws an error that it have no match for * but for &

    but when i change TDesC * A; to & and get this code

    Code:
    TDesC & A; - error here
    gc.DrawText(A,TPoint(30,60));
    i get ERROR: const or & variable need initializer
    That is C++
    * : pointer, the address of something. It can be null or anything else, so it has to be checked, and it can be modified
    & : reference, practically it is a new name for an already existing variable. It has to be initialized, that is what the compilar is missing.
    Code:
    int a=6;
    int &b(a);
    b and a are the same variable now. If you set one of them, the other one changes. If you check their address in the memory, they are going to be the same.
    This is why Symbian C++ methods like receiving references, they always refer to something.

    HBufC inherits TDesC, so you can write
    Code:
    char *hello="hello";
    HBufC *helloDes=AnsiToHBufLC(hello);
    gc.DrawText(*helloDes,TPoint(30,60));
    CleanupStack::PopAndDestroy(); // helloDes
    Note that Draw is actually not an L/C method, in fact it should not work with memory management and things like that. So *helloDes should be *iHelloDes, a member variable, converted somewhere else.

  4. #4
    Registered User
    Join Date
    Jul 2013
    Posts
    73

    Re: char array to TDesC

    Sweet! i made it,
    had to change from static_cast to reinterpret_cast and make it convert from static array of fixed size; anyway i post a code for other people :P


    oh about these include files i dont know which to include so i included all ;x
    Code:
    file stringunit.h code is not perfect but i will handle it in future.
    
    
    #ifndef STRINGUNIT_H
    #define STRINGUNIT_H
    
    #include <aknviewappui.h>
    #include <eikappui.h>
    #include <eikedwin.h>
    #include <aknutils.h>
    #include <AknAppUi.h>
    #include <aknlists.h>
    #include <barsread.h>
    #include <aknnotewrappers.h>
    #include "e32cmn.h"
    
    
    
    struct AnsiString {
    public:
    	char text[1024];
    	int len;
    
    	
    	AnsiString (char * aString)
    		{
    	
    	TPtrC8 str(reinterpret_cast<const TUint8*>(aString));
    len = str.Length();
    int i;
    
    for (i=0; i < len; i++) text[i] = aString[i];
    
    	
    		}
    	
    /*	AnsiString(const AnsiString & creator)
    		{
    *this = creator;
    		}	*/
    	
    	AnsiString &operator=(const TDesC & ain) 
    		{
    len = ain.Length();
    int i;
    
    for (i=0; i < len; i++) text[i] = ain[i];
    
    			return *this;
    		}
    	
    	
    	
    	AnsiString &operator=(AnsiString ain) 
    		{
    len = ain.len;
    int i;
    
    for (i=0; i < len; i++) text[i] = ain.text[i];
    
    			return *this;
    		}	
    	
    	
    
    	AnsiString &operator=(const char * ain) 
    		{
    		TPtrC8 str(reinterpret_cast<const TUint8*>(ain));
    len = str.Length();
    int i;
    
    for (i=0; i < len; i++) text[i] = ain[i];
    
    			return *this;
    		}
    
    
    
    	AnsiString operator+(const char * ain) const
    		{
    		AnsiString tmp;
    		tmp = *(this);
    		int i;
    		
    		TPtrC8 str(reinterpret_cast<const TUint8*>(ain));
    		int oldlen = len;
    		int cnt = -1;
    		tmp.len = len + str.Length();
    
    
    for (i=0; i < len; i++) 
    	tmp.text[i] = text[i];
    
    for (i=oldlen-1; i < tmp.len; i++)  {
    	cnt = cnt + 1;
    	tmp.text[i] = ain[cnt];
    	
    								}
    
    return AnsiString(tmp.text);
    		}
    
    	
    	
    	AnsiString operator+(AnsiString ain) const
    		{
    		AnsiString tmp;
    		tmp = *(this);
    		int i;
    		char *aString = new char[ain.len];
    		
    	    for (i=0; i < ain.len; i++) aString[i] = ain.text[i];
    		
    		TPtrC8 str(reinterpret_cast<const TUint8*>(aString));
         
    		int oldlen = len;
    		int cnt = -1;
    		
    		tmp.len = len + str.Length();
    		
    		for (i=0; i < len; i++) 
    			tmp.text[i] = text[i];
    
    for (i=oldlen-1; i < tmp.len; i++)  {
    	cnt = cnt + 1;
    	tmp.text[i] = aString[cnt];
    	
    									}
    
    return AnsiString(tmp.text);
    		}
    
    
    	
    	
    	HBufC *AnsiStringToHBufLC()
    	{
    		
    		int i;
    		char *aString = new char[len];
    		
    	    for (i=0; i < len; i++) aString[i] = text[i];
    		
    		TPtrC8 str(reinterpret_cast<const TUint8*>(aString));
    	    HBufC *ret=HBufC::NewLC(str.Length()); 	
    	    ret->Des().Copy(str);
    	    return ret;
    	}
    	
    
    	
    	AnsiString()
    		{
    	
    	len = 0;
    	int i;
    	for (i=0; i < 1024; i++) text[i] = 'x';
    	
    		}
    	
    	
    	
    	
    	
    	
    };
    /*
    
    AnsiString BooltoStr(bool enabled)
    	{
    	char *aString="hello";
    	TPtrC8 str(reinterpret_cast<const TUint8*>(aString));
    	
    	
    	int i;
    	}
    
    */
    
    
    
    #endif

    and the execution thing:



    Now i just need to handle numbers, comparing strings and finding substr will be too easy after it


    Don't tell me this handling isn't eaisier :P

    cheers.
    Last edited by melepeta; 2013-09-23 at 23:00.

Similar Threads

  1. convert char* to TDesC&
    By horan in forum Symbian
    Replies: 3
    Last Post: 2011-04-06, 13:15
  2. From char* to TDesC
    By logan04x in forum Open C/C++
    Replies: 2
    Last Post: 2009-01-29, 12:34
  3. TDesC to char* problem
    By _nomad_ in forum Symbian
    Replies: 6
    Last Post: 2006-08-23, 19:38
  4. convert char* to TDesC&...
    By sbleriot in forum Symbian
    Replies: 1
    Last Post: 2002-12-16, 11:48
  5. converting char* to tdesc
    By ram9210 in forum Symbian
    Replies: 2
    Last Post: 2002-06-06, 08:33

Posting Permissions

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