wxString conversion Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 324
Joined: Mon Oct 06, 2008 12:55 pm

wxString conversion

Post by samsam598 » Tue Nov 10, 2009 7:18 am

From other thread when I touched the conversion between wxString and char/wxChar...,I met a new problem:

Given below code:

Code: Select all


    wxString text="This is a text";
    const char* p1=text.mb_str();
    printf("The second character is %c\n",*++p1);//prints 'h',fine
    printf("%s\n",p1);//prints "his is a text",fine 
    
    const char* p2=(const char*)text.c_str();
    printf("The second character is %c\n",*++p2);//prints 'h',fine
    printf("%s\n",p2);//prints "his is a text",fine

    
    //const wxChar* p3=text.wx_str();//this one single line is enough acutally
    const wxStringCharType* p3=text.wx_str();
    const wxChar* p4=p3;
    printf("The second character is %c\n",*++p4);//prints 'h',fine
    printf("%s\n",p3);//prints 'T',not "This is a text"??
    printf("%s\n",p4);//prints 'h',not "his is a text"??
    
My question is to the last 2 lines of code:

Code: Select all

    printf("%s\n",p3);
    printf("%s\n",p4);
why the behavior is not the same as char* type?

Thanks for your help in advance.

Regards,
Sam
Regards,
Sam
-------------------------------------------------------------------
Windows xp
VS.Net 2003/MinGW 3.4.5 C::B character set: UTF-8
wxWidgets github 3.0 RC1 Unicode Static build,Unicode Shared build.

Romas
I live to help wx-kind
I live to help wx-kind
Posts: 176
Joined: Mon Jun 16, 2008 11:07 am
Location: Kaunas

Post by Romas » Tue Nov 10, 2009 8:53 am

Is your project unicode? If yes, I think you do not understand the meaning of the "unicode".

When you do print ("%s", unicode_str); Your unicode string will look in bytes (and chars): T 00 h 00 i 00 s 00. In hex :)

When you call print, it will print your string until it meet first occurance of \0 character, thus, it will be after T. If you call print ("%S", unicode_str) you will get the whole string.
Everything requires a line of code.

TrV
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 630
Joined: Wed Jul 04, 2007 1:12 pm

Re: wxString conversion

Post by TrV » Tue Nov 10, 2009 9:55 am

samsam598 wrote:why the behavior is not the same as char* type?
Because i think wxChar and wxStringCharType (didn't know the latter) are not just wrappers for char.
Remember char* is just a dirty workaround to handle char strings in pure C.
Even in pure C++, the use of char* for char strings is discouraged, and one has to use STL (string class).

Apart from that, is there some kind of issue you want to deal with?

samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 324
Joined: Mon Oct 06, 2008 12:55 pm

Re: wxString conversion

Post by samsam598 » Tue Nov 10, 2009 10:02 am

TrV wrote: Apart from that, is there some kind of issue you want to deal with?
No.Just wanna find a simple,easy way to handle strings and characters.

Thank you.


To both:

I accidently marked reply by myself as Accepted and then I deleted that post (by myself),and now I found that I can not get back the "accept" button any more.Oh my!

Sorry~

EDIT: I've 'unaccepted' the topic. DavidHart
Regards,
Sam
-------------------------------------------------------------------
Windows xp
VS.Net 2003/MinGW 3.4.5 C::B character set: UTF-8
wxWidgets github 3.0 RC1 Unicode Static build,Unicode Shared build.

TrV
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 630
Joined: Wed Jul 04, 2007 1:12 pm

Re: wxString conversion

Post by TrV » Tue Nov 10, 2009 10:26 am

samsam598 wrote:No.Just wanna find a simple,easy way to handle strings and characters.
Why not use native wxString methods? Like [] and so on?

samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 324
Joined: Mon Oct 06, 2008 12:55 pm

Re: wxString conversion

Post by samsam598 » Tue Nov 10, 2009 10:53 am

TrV wrote:
samsam598 wrote:No.Just wanna find a simple,easy way to handle strings and characters.
Why not use native wxString methods? Like [] and so on?
For one ,just want to get an idea under what situation should I /would I /have to use wxChar/char i/o wxString basis,still unware about it after this test;

For two,don't know it is not as simple,easy as expected after this test.:P

For three,not sure can one ignore char/wxChar at all,just use string/wxString instead?

Regards,
Sam
Regards,
Sam
-------------------------------------------------------------------
Windows xp
VS.Net 2003/MinGW 3.4.5 C::B character set: UTF-8
wxWidgets github 3.0 RC1 Unicode Static build,Unicode Shared build.

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Tue Nov 10, 2009 4:54 pm

Code: Select all

const char* p1=text.mb_str(); 
This is wrong; mb_str returns a temporary pointer, don't store it.

See http://wiki.wxwidgets.org/Converting_ev ... m_wxString
"Keyboard not detected. Press F1 to continue"
-- Windows

jfouche
Super wx Problem Solver
Super wx Problem Solver
Posts: 442
Joined: Tue May 06, 2008 4:52 pm
Location: France

Post by jfouche » Tue Nov 10, 2009 9:25 pm

This topic can help you to understand the problem with mb_str() method.
Jérémie

ninja9578
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 236
Joined: Thu Jan 29, 2009 3:33 pm

Post by ninja9578 » Tue Nov 10, 2009 9:56 pm

As for why your printf is not working try this:

if (sizeof(char) != sizeof(wxChar)) wxMessageBox(_T("wxChar != char"));

samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 324
Joined: Mon Oct 06, 2008 12:55 pm

Post by samsam598 » Wed Nov 11, 2009 12:30 am

Hi all,

Thank you so .. much for all your help!

Regards,
Sam
Regards,
Sam
-------------------------------------------------------------------
Windows xp
VS.Net 2003/MinGW 3.4.5 C::B character set: UTF-8
wxWidgets github 3.0 RC1 Unicode Static build,Unicode Shared build.

Post Reply