Conversion from wide to single-byte:Size_t Wcstombs ( Char *Mbstr, Const Wchar_t *Wcstr, Size_t Count );
Single-byte to wide byte:Size_t Mbstowcs ( Wchar_t *Wcstr, Const Char *Mbstr, Size_t Count );
The above two are standard C ++, and the following two seem to be Microsoft functions. However, the two functions can only be converted one by one, that is, only one character can be converted at a time. The following two methods are the conversion between a wide string and a single-byte string.
Wide-byte to multi-byte: WideCharToMultiByte // In fact, the multi-byte here is what we call char
Multi-byte to wide byte: MultiByteToWideChar // multi-byte, that is, ASCII single byte
These four methods.
The following example shows how to convert a wide string to a char string.
BSTR devname; // olewchar is actually wchar
Char * name = NULL;
PNCP-> get_Name (& devname );
DWORD n = WideCharToMultiByte (CP_OEMCP, NULL, devname,-1, NULL, 0, NULL, FALSE );
Name = new char [n];
WideCharToMultiByte (CP_OEMCP, NULL, devname,-1, name, n, NULL, FALSE );
Cout <name <endl;
Delete [] name;
The following describes how to convert a char string to a unicode string, that is, a wide character.
Char sText [20] = {"actually I am a so-called multi-byte string "};
DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, sText,-1, NULL, 0 );
Wchar_t * pwText;
PwText = new wchar_t [dwNum];
MultiByteToWideChar (CP_ACP, 0, pwText,-1, sText, dwSize );
If (! PwText ){
Delete [] pwText;
}