×

Discussion Board

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

    Strange text display

    Hello i am working on a source file that will make (for me) use of strings (descriptors) way eaisier, but i have a problem that i can not solve.

    It draws a text 'Hello World.' + some strange characters instead of just 'Hello World.'




    The drawing routine:
    Code:
    AnsiString p;
    p = "Hello World.";
    
    gc.DrawText(*p.AnsiStringToHBufLC(),TPoint(30,90));
    CleanupStack::PopAndDestroy();

    Code:
    struct AnsiString {
    public:
    	TUint8 text[1024];
    	int len;
    
    
    	AnsiString &operator=(const char * ain) 
    		{
    		TPtrC8 str((const TUint8*)(ain));
    len = str.Length();
    int i;
    
    for (i=0; i < len; i++) text[i] = ain[i];
    
    			return *this;
    		}
    
    
    	HBufC *AnsiStringToHBufLC()
    	{
    		
    		int i;
    		TUint8 *aString = new TUint8[len];
    		
    	    for (i=0; i < len; i++) aString[i] = text[i];
    		
    //	TPtrC8 str(reinterpret_cast<const TUint8*>(aString));
    		TPtrC8 str((const TUint8*)(aString));
    	    HBufC *ret=HBufC::NewLC(str.Length()); 	
    	    ret->Des().Copy(str);
    	    return ret;
    	}
    
    };

    I post a question because i do not see any bugs in the code. ;]

    cheers.

  2. #2
    Registered User
    Join Date
    May 2008
    Location
    Surat Thani
    Posts
    258

    Re: Strange text display

    Quote Originally Posted by melepeta View Post

    Code:
    	HBufC *AnsiStringToHBufLC()
    	{		
    	    int i;
    	    TUint8 *aString = new TUint8[len];
    		
    	    for (i=0; i < len; i++) aString[i] = text[i];
    
    	    TPtrC8 str((const TUint8*)(aString)); 
    	    HBufC *ret=HBufC::NewLC(str.Length()); 	
    	    ret->Des().Copy(str);
    	    return ret;
    	}
    };
    The function AnsiStringToHBufLC passes a byte array without a zero byte in the end to the TPtrC8 constructor, which assumes that the array passed to it is zero terminated. So, whatever garbage happens to be in the memory after aString until the next zero byte is considered to be a part of str. TPtrC8 has another constructor, which takes a length parameter addition to the bytes, you might want to try that. The function also has a memory leak - aString byte array is allocated but not freed.

    Btw, are you sure you really want to hard code 8-bit strings in the code and do these conversions every time when drawing, instead of using descriptors with full unicode support and localizable texts from the application resource file?

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

    Re: Strange text display

    Thanks, i didn't know that. Now it works.

    Code:
    	HBufC *AnsiStringToHBufLC()
    	{
    	
    		int i;
    		TUint8 *aString = new TUint8[len+1];
    
    	    for (i=0; i < len; i++) aString[i] = text[i];
    	    aString[len] = NULL;
    		TPtrC8 str((const TUint8*)(aString)); 
    		
    
    		
    		HBufC *ret=HBufC::NewLC(str.Length()); 	
    	    ret->Des().Copy(str);
    	    delete aString;
    	    return ret;
    	}

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,569

    Re: Strange text display

    aString becomes obsolete as soon as you check other TPtrC8 consturctors, like TPtrC8(const TUint8 *aBuf, TInt aLength)

    PS: note that descriptors are rather powerful things, so if your main goal is to have string manipulations, like concatenation, mixing types, etc., that is already there
    Code:
    _LIT(KHello,"Hello World ");
    
    TBuf<1024> buf;
    buf=KHello;
    buf.AppendNum(123);
    gc.DrawText(buf,TPoint(30,90));
    Last edited by wizard_hu_; 2013-10-05 at 12:42.

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

    Re: Strange text display

    I rather want to use something liek this
    AnisString k = 'hello';
    AnsiString k2 = 'world';

    k = k + k2;
    drawtext(*K.ansistrigntohbufc);


    or in this case;


    Code:
    int num = 123;
    AnsiString KHello = 'Hello World';
    KHello = KHello + IntToStr(num);
    gc.DrawText(K*Hello.AnsiStringToHBufLC(),TPoint(30,90));
    ;]


    Its just becasue i am programing in C++ Builder for more than 12 years. And i like its syntax.

  6. #6
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,569

    Re: Strange text display

    Quote Originally Posted by melepeta View Post
    Its just becasue i am programing in C++ Builder for more than 12 years. And i like its syntax.
    Ok, actually this was the important part, you want to follow a particular syntax you are familiar with. Just so far it seemed like if you were going to re-invent the wheel, warm water, and everything from scratch for no apparent reason...
    A thing you may want to try/use is the Open C/C++ plug-in (actually it is built-in for the last 4-5 Symbian SDK-s). It was about providing a more complete set of standard C and C++ (including STL) libraries than the default set provided with Symbian (which is a rather crippled stdlib, so C-only).
    It has some support functions, see http://developer.nokia.com/Community...ring_Utilities for a quick impression.

    And a practical thing: while the plug-in is integrated in newer SDK-s, the examples and documentation may be missing. So you may still want to get the plug-in version from https://developer.nokia.com/Develop/...ian_C++/Tools/ (I give this link because you may find the other extra API-s also interesting). I do not remember exactly, but it may require a compatible SDK for installation, you can find older Symbian/S60 SDK-s in the Tools Archive, the link is https://developer.nokia.com/info/sw....16-3.0-mr.html.

Similar Threads

  1. Replies: 2
    Last Post: 2010-09-28, 07:41
  2. strange display problem
    By westweb in forum Symbian
    Replies: 0
    Last Post: 2007-07-03, 04:24
  3. Nokia 7650 display strange error
    By RongJia in forum Mobile Java General
    Replies: 1
    Last Post: 2005-06-06, 03:26
  4. How display hindi text in text editor
    By debasish1234 in forum Symbian
    Replies: 0
    Last Post: 2004-10-08, 09:12
  5. Replies: 0
    Last Post: 2004-10-07, 08:57

Posting Permissions

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