Page 1 of 1

WxString conversion question.

Posted: Mon Aug 09, 2010 5:45 pm
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.

Posted: Mon Aug 09, 2010 6:15 pm
by Auria
The wiki article does have "char* to wxString", is that not what you want?

Posted: Mon Aug 09, 2010 6:32 pm
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.

Re: WxString conversion question.

Posted: Mon Aug 09, 2010 6:35 pm
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());

Re: WxString conversion question.

Posted: Mon Aug 09, 2010 6:48 pm
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.

Posted: Mon Aug 09, 2010 6:49 pm
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.

Re: WxString conversion question.

Posted: Mon Aug 09, 2010 6:53 pm
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

Posted: Mon Aug 09, 2010 6:58 pm
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];

Posted: Mon Aug 09, 2010 7:03 pm
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

Posted: Mon Aug 09, 2010 7:07 pm
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.

Posted: Mon Aug 09, 2010 7:07 pm
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).

Posted: Mon Aug 09, 2010 7:09 pm
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?

Posted: Mon Aug 09, 2010 7:16 pm
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.

Posted: Mon Aug 09, 2010 7:25 pm
by Dark Alchemist
Alrighty doublemax. :)

Thanks everyone for the help as I appreciate it.