| gunesahmet (4) | |
|
I want to convert my string to whcar_t. I try this code but i can't success :S string username="UserName"; wstring widestr = wstring(username.begin(), username.end()); wchar_t* widecstr = widestr.c_str(); | |
|
Last edited on
|
|
| Disch (8349) | |||
|
There some narrow->wide conversion function somewhere but I can never remember where it is. The easy/basic way to do it is just do a dumb copy:
The better question, though, is why do you want to do this? If you are using wide strings... then use wide strings from the start. And if you aren't using wide strings, then why bother converting to them? | |||
|
|
|||
| Duoas (6734) | |||||
|
It is actually a common need to cast between string types. You can use std::copy() or you can use a wstring constructor:
This is just off the top of my head. I'm sure there are better ways to do it. | |||||
|
|
|||||
| JLBorges (1336) | |||
Or if you are using GCC (which does not have std::wstring_convert), std::setlocale() followed by std::mbsrtowcs() http://en.cppreference.com/w/cpp/string/multibyte/mbsrtowcs
| |||
|
|
|||
| vince1027 (137) | |||
If your C++ compiler supports C99 features, you can do one without the temporary std::wstring:
Obviously, you can still do it without C99 feature support by allocating the wchar_t array on the heap. Your mileage will vary. | |||
|
Last edited on
|
|||