15

I'm kind of new to using Unicode string and pointers and I've no idea how the conversion to unicode to ascii and versa-versa works. Following is what I'm trying to do,

const wchar_t *p = L"This is a string";

If I wanted to convert it to char*, how would the conversion work with converting wchar_t* to char* and vice-versa?

or by value using wstring to string class object and vice-versa

std::wstring wstr = L"This is a string";

If i'm correct, can you just copy the string to a new buffer without conversion?

user963241
  • 6,758
  • 19
  • 65
  • 93

6 Answers6

23

In the future (VS 2010 already supports it), this will be possible in standard C++ (finally!):

#include <string>
#include <locale>

std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
const std::wstring wide_string = L"This is a string";
const std::string utf8_string = converter.to_bytes(wide_string);
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Philipp
  • 48,066
  • 12
  • 84
  • 109
5

The conversion from ASCII to Unicode and vice versa are quite trivial. By design, the first 128 Unicode values are the same as ASCII (in fact, the first 256 are equal to ISO-8859-1).

So the following code works on systems where char is ASCII and wchar_t is Unicode:

const char* ASCII = "Hello, world";
std::wstring Unicode(ASCII, ASCII+strlen(ASCII));

You can't reverse it this simple: 汉 does exist in Unicode but not in ASCII, so how would you "convert" it?

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • There is also from_bytes which you can use like --- std::wstring_convert> converter; const std::wstring wstring = converter.from_bytes(string); – TinyRacoon Oct 22 '20 at 16:14
3

C++ by itself doesn't offer this functionality. You'll need a separate library, like libiconv.

Thomas
  • 174,939
  • 50
  • 355
  • 478
3

The solutions are platform-dependent. On Windows use MultiByteToWideChar and WideCharToMultiByte API functions. On Unix/linux platforms iconv library is quite popular.

Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121
  • Beware that `MultiByteToWideChar` has a bug when converting codepage 50225 (Korean - ISO-2022-KR) which converts characters incorrectly as noted on https://support.microsoft.com/en-us/kb/960293 - The suggested workaround is to use `IMultiLanguage::ConvertStringToUnicode` instead which converts the same characters properly - please update answer to make this more visible. – Coder12345 Sep 02 '15 at 12:52
3

C Standard library functions: mbstowcs and wcstombs

cpx
  • 17,009
  • 20
  • 87
  • 142
0

The widen() algorithm converts char to wchar_t :

char a;
a = 'a';
whcar_t wa = cin.widen(a);

Of course, you have to put it into a loop. And resolve the *; The opposite is accomplished by narrow()

simon
  • 1,180
  • 3
  • 12
  • 33
bratao
  • 1,980
  • 3
  • 21
  • 38