Author Topic: wxString to std::string question  (Read 10675 times)

Offline manmach

  • Multiple posting newcomer
  • *
  • Posts: 39
wxString to std::string question
« on: August 31, 2006, 09:37:19 am »
I have been trying all kinds of keywords on Google to find out how I can convert a wxString to std::string, but have yet to find anything at all on the subject. Which is weird, because I would expect it to be fairly common if you want to use other libraries with wxWidgets.

So I am hoping at least one of you has this experience and is willing to share.

I need to take a url from a text control and pass it to a library that expects a std::string. I can get the url from the control using GetValue, but that gives me a wxString.
I have tried to use wxString::c_str(), but that gives me a compilation error:

Quote
error: conversion from `const wxChar*' to non-scalar type `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' requested

I am using wxWidgets 2.6.3 unicode and Codeblocks with Mingw.

Offline tiwag

  • Developer
  • Lives here!
  • *****
  • Posts: 1196
  • sailing away ...
    • tiwag.cb
Re: wxString to std::string question
« Reply #1 on: August 31, 2006, 10:29:06 am »
this forum is related to Code::Blocks,

please ask your wxWidgets related questions there

http://wxforum.shadonet.com/

Offline Dahman

  • Multiple posting newcomer
  • *
  • Posts: 16
Re: wxString to std::string question
« Reply #2 on: August 31, 2006, 10:34:22 am »
std::string std_string = wx_string.c_str();

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: wxString to std::string question
« Reply #3 on: August 31, 2006, 10:56:46 am »

Quote
I need to take a url from a text control and pass it to a library that expects a std::string. I can get the url from the control using GetValue, but that gives me a wxString.
I have tried to use wxString::c_str(), but that gives me a compilation error:

Quote
error: conversion from `const wxChar*' to non-scalar type `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' requested

I am using wxWidgets 2.6.3 unicode and Codeblocks with Mingw.
The problem is that you are using unicode, and your API expects const char*.

You need to convert the string.

Here is a link to unicode:
http://wiki.codeblocks.org/index.php?title=Unicode_Standards

And in short, what you need is:

Add this section to one of your headers (a one that is included by all)
Code
#ifdef wxUSE_UNICODE
#define _U(x) wxString((x),wxConvUTF8)
#define _UU(x,y) wxString((x),y)
#define _CC(x,y) (x).mb_str((y))
#else
#define _U(x) (x)
#define _UU(x,y) (x)
#define _CC(x,y) (x)
#endif

#define _C(x) _CC((x),wxConvUTF8)

And in your code, whenever your API is requiring a const char*, do this:

Code
const wxCharBuffer pbuff = _C(wx_string); // convert wxString to wxCharBuffer
foo(pbuff.data()); // get the data as cnost char*

Eran

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2750
Re: wxString to std::string question
« Reply #4 on: August 31, 2006, 03:23:35 pm »
this forum is related to Code::Blocks,

please ask your wxWidgets related questions there

http://wxforum.shadonet.com/

Personally, I wouldn't recommend shadonet to anyone. For a year, I've left  questions/fixes/suggestions etc. there, and have never received an answer. The majority of questions get 0 responses.

Plus, the developers stay on their mailing lists and never visit the users forum.

There's got to be a better way to get an answer about wxWidgets than shadonet.
« Last Edit: August 31, 2006, 03:32:55 pm by Pecan »

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: wxString to std::string question
« Reply #5 on: August 31, 2006, 06:16:54 pm »
Quote
There's got to be a better way to get an answer about wxWidgets than shadonet.

The best way is to register to the wxWidgets mailing list (not the developers)

VZ (Vadim Zeitlin) will probably answer your question :wink:, this guy is a machine gun when it comes to answers questions related to wxwidgets

Eran

Offline Game_Ender

  • Lives here!
  • ****
  • Posts: 551
Re: wxString to std::string question
« Reply #6 on: August 31, 2006, 07:43:52 pm »
Pecan exzagerated, not all questions get answered, but I would say at least half of them do and almost all get a response (just go look for yourself).  Bad mouthing and not going to a place that is trying to be the official wxWidgets forum will do nothing for its growth or usability.

EDIT, I removed the word bad from the sentence above, I don't know why its there.
« Last Edit: August 31, 2006, 08:23:27 pm by Game_Ender »

Offline tiwag

  • Developer
  • Lives here!
  • *****
  • Posts: 1196
  • sailing away ...
    • tiwag.cb
Re: wxString to std::string question
« Reply #7 on: August 31, 2006, 08:06:45 pm »
Pecan exzagerated, not all bad questions get answered, but I would say at least half of them do and almost all get a response ...

are you exaggerating ?  :)

Offline Game_Ender

  • Lives here!
  • ****
  • Posts: 551
Re: wxString to std::string question
« Reply #8 on: August 31, 2006, 08:27:07 pm »
Sorry about that stray "bad" in there.  I don't think I am, you can browse the forums yourself the little check mark means the person who asked the question thinks they got an answer and I barely see any posts with "0" responses.