WxString conversion question. 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
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

WxString conversion question.

Post by Dark Alchemist »

How do I do the opposite of this?

Code: Select all

char cstring[1024];
// assuming you want UTF-8, change the wxConv* parameter as needed
strncpy(cstring, (const char*)mystring.mb_str(wxConvUTF8), 1023);
I need to take a char X[1024] and slap it into a WxString. I was hoping the bookmarked http://wiki.wxwidgets.org/Converting_ev ... m_wxString site would have it but nope (I always hope that page gets updated for weirdness like this).

What I currently do is simply wxString Z = X; but sometimes I do crash when doing this. X will have a null terminator in it so that shouldn't be the issue.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

The wiki article does have "char* to wxString", is that not what you want?
"Keyboard not detected. Press F1 to continue"
-- Windows
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Post by Dark Alchemist »

Not exactly because it is using char * whereas I am using a char block. It has a wxstring into a char block but not a char block into a wxstring which is what I need.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: WxString conversion question.

Post by evstevemd »

Dark Alchemist wrote:How do I do the opposite of this?
What do you want to do?
I find Printf very handy

Code: Select all

wxString str;
wxString str2 = wxT("Is this what you want?");
str.Printf(wxT("Test String: %s"), str2.c_str());
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Re: WxString conversion question.

Post by Dark Alchemist »

evstevemd wrote:
Dark Alchemist wrote:How do I do the opposite of this?
What do you want to do?
I find Printf very handy

Code: Select all

wxString str;
wxString str2 = wxT("Is this what you want?");
str.Printf(wxT("Test String: %s"), str2.c_str());
Trying to correctly convert a char block (like char X[1024]) into a wxstring.

The page I posted a link to tells us how to convert FROM a wxstring INTO a char block but not the other way around which is what I need.
mathieumg
Earned a small fee
Earned a small fee
Posts: 17
Joined: Fri Aug 06, 2010 5:16 pm

Post by mathieumg »

http://wiki.wxwidgets.org/Converting_ev ... o_wxString ?

Edit: Oops, it was already pointed out. However, if by "char block" you mean char array, well that's what char * is.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: WxString conversion question.

Post by evstevemd »

Dark Alchemist wrote:
evstevemd wrote:
Dark Alchemist wrote:How do I do the opposite of this?
What do you want to do?
I find Printf very handy

Code: Select all

wxString str;
wxString str2 = wxT("Is this what you want?");
str.Printf(wxT("Test String: %s"), str2.c_str());
Trying to correctly convert a char block (like char X[1024]) into a wxstring.

The page I posted a link to tells us how to convert FROM a wxstring INTO a char block but not the other way around which is what I need.
AFAIK char cstring[1024] is like saying same as char* cstring but specifying size. If I'm righ then I think this will work

Code: Select all

wxString str;
char cstring[1024];
//.....asign some value to cstring
str.Printf(wxT("Test String: %s"), char);
try and post whatever error it gives. I will try to come with solution in minutes
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Post by Dark Alchemist »

I use the wxstring.printf all the time but is it safe to use it for a char array (said block but meant array)?

It works but is it as safe as I can get it? Probably safer than just wxstring = char[X];
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Post by evstevemd »

Dark Alchemist wrote:I use the wxstring.printf all the time but is it safe to use it for a char array (said block but meant array)?

It works but is it as safe as I can get it? Probably safer than just wxstring = char[X];
I'm not sure of safety. I remember sometimes ago I used something but wasn't printf. Would you try this and see if it works

Code: Select all

wxString mystring = wxString::Format(wxT("%s"),cstring);
If it works, I guess it is safe
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
doublemax
Moderator
Moderator
Posts: 19102
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

The problem with the printf or wxString::Format solutions is that you have no control over character encoding.

These two lines do exactly the same thing. chars is a "char *".

Code: Select all

char* chars = "Hello world";
char chars[] = "Hello world";
So conversion like given in the wiki page is the way to go.
Use the source, Luke!
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Post by Dark Alchemist »

I thought the format and the printf were one in the same? I know my time sensitive routine took a serious hit with the printf and that format would mean I would be recreating string with each pass and that would be extremely slow (I tried just wxstring A; a.printf and the wxstring A was killing me).
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Post by Dark Alchemist »

doublemax wrote:The problem with the printf or wxString::Format solutions is that you have no control over character encoding.

These two lines do exactly the same thing. chars is a "char *".

Code: Select all

char* chars = "Hello world";
char chars[] = "Hello world";
So conversion like given in the wiki page is the way to go.
Yes, the encoding would be an issue and how long would it take to do the conversion is of primary concern (second to crashing of course).

Oh, is there a way I can define my wxstring outside of the time sensitive loop and still do the utf8 conversion with it each pass?
User avatar
doublemax
Moderator
Moderator
Posts: 19102
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

I don't see character encoding conversion as a serious performance issue. I would think the loading time from hard disc is the limiting factor, the conversion should only be a fraction of it.

But if you *need* a conversion, there's no way around it anyway, unless you can always provide the text in the native character encoding for the current platform, e.g. UCS2 on MSW.

Without knowing the exact task it's hard to give suggestions for performance inprovement.
Use the source, Luke!
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Post by Dark Alchemist »

Alrighty doublemax. :)

Thanks everyone for the help as I appreciate it.
Post Reply