views:

534

answers:

4

I have a situation where I need to compare a char* with a WideString. How do I convert the WideString to a char* in C++?

+5  A: 

You can use the wcstombs function.

size_t wcstombs ( char * mbstr, const wchar_t * wcstr, size_t max );

Varuna
This got me over the finish line:System::WideString wstr = "something";char* buffer = new char[100];wcstombs(buffer, wstr.data(), 100);thanks.
Seth
+2  A: 

See WideCharToMultiByte() and MultiByteToWideChar().

Gonzalo
This solution is Windows-Only though.
Billy ONeal
Yes, the C99 wcstombs()/mbstowcs() are covered in other answers.
Gonzalo
Billy, so is the System::WideString type that's the topic of this question.
Rob Kennedy
+1  A: 

Not possible. Actually, you mixed two different concepts:

  • Widestring implies a buffer in UTF-16 encoding.
  • char* may contain anything, from UTF-8 to ASCII-only text (in which case, this is convertable only if your widestring does not contain non-ASCII characters).

Please see my answer to http://stackoverflow.com/questions/1049947/should-utf-16-be-considered-harmful about how to properly handle text.

Pavel Radzivilovsky
wchar_t is defined to contain UTF-16 encoded data only on Windows. It could be different for other OS's. In general, it is locale dependent.
mch
I have a thirdpartycomponent which provides a WideString. However, I need to compare that with a std::string so if it is not possible what options do I have?
Seth
Convert the other way around, then compare.
Pavel Radzivilovsky
+1  A: 

To compare a System::WideString object with a char* (as the question body says you want to do), you can create a new WideString object from the pointer and then use ordinary == operator. The class has several constructors, including one for const char*.

char* foo = ...;
WideString bar = ...;
if (WideString(foo) == bar)
  std::cout << "They're equal!\n";

In fact, as long as the WideString object is on the left, you don't even need the constructor call because the pointer will be converted automatically.

if (bar == foo) { ... }


To convert a WideString to a char* (as the question title says you want to do), you might consider using the AnsiString type. They're convertible between each other. To get the ordinary pointer, call the c_str method, just like you would with std::string.

WideString bar = ...;
AnsiString foo = bar;
std::cout << foo.c_str();
Rob Kennedy
A *space*? That can't be right. A null character would cause truncation; if you have one that's not at the real end of the string, then use the two-argument version of the constructor so you can specify the real length. A space might cause truncation if you had something in a `std::istringstream` and you were reading a string from it with `stream >> stringvar`.
Rob Kennedy
you were right however I did get truncation when I did this `WideString wstr_2 = mTreeView->Text[Node][0];` `WideString lowercase = wstr_2.LowerCase();` that's why I want to work with `std::string`. In the debugger wstr_2 appears to be correct and then when the `LowerCase()` function is applied it gets truncated.
Seth