1. multibyte shows to us as char *. while in fact, it can be any code page encoding, including GBK, utf8, etc. if a char * represents utf8 characters, we need to handle it specially in below way:
// Convert Unicode (Windows Default UTF16) to utf8 char * string.
Makeutf8string (const wchar_t * pwide)
{
// Compute the required size of the buffer by passing cbmultibyte as 0
Mcad_assert (pwide );
Int nbytes =: widechartomultibyte (cp_utf8, 0, pwide,-1, null, 0, null, null );
Mcad_assert (nbytes> 0 );
Char * pnarrow = new char [nbytes + 1];
Nbytes =: widechartomultibyte (cp_utf8, 0, pwide,-1, pnarrow, nbytes + 1, null, null );
Utxstring8 utf8str (pnarrow );
Mcad_assert (nbytes );
Delete [] pnarrow;
Return utf8str;
}
// Convert a utf8 char * To be UTF16 Unicode string wchar *.
Makewidestring (const char * pnarrow)
{
// Get the required size of the buffer that matches es the Unicode string.
DWORD dwminsize;
Dwminsize = multibytetowidechar (cp_utf8, 0, pnarrow,-1, null, 0 );
// Convert headers from ASCII to Unicode.
Wchar * pwide = new wchar [nbytes + 1];
Multibytetowidechar (cp_utf8, 0, pnarrow,-1, pwide, dwminsize );
// Below method can work as abve code section:
// Setlocale (lc_all, ". 65001/. utf8"); // utf8
// Mbstowcs
}
2. My hot Summary of widechartomultibyte () vs. wcstombs () Kind Comparison:
1) wcstombs () CILS widechartomultibyte () inside. widechartomultibyte () is the basic function to do the conversion. but because of its too when too detail arguments, CRT provides the instead function to operate our work. very good consideration!
2) When call widechartomultibyte (), there is an argument by which we can set the code page to be used to do the conversion. while for wcstombs (), It just use current system locale. so in order to use these 2 Functions really in the same place, setlocale must be called before wcstombs ().
Resource related:
Wcstombs and mbstowcs
Multibytetowidechar
The http://msdn.microsoft.com/en-us/library/ms776413 (vs.85). aspx
Note:For UTF-8,DwflagsMust be set to either 0 or mb_err_invalid_chars. Otherwise, the function fails with error_invalid_flags.
Mbstowcs
Http://msdn.microsoft.com/en-us/library/k1f9b8cy (vs.71). aspx