How to deal with the messy code when using MariaDB for Chinese

For compatibility, using the code of UTF-8.

Due to the code of VS using GBK2312, so must transfer the UTF-8 to mysql_query.

I use the code of this:

 1 CStringW trans2CW(const char * wch, int type = CP_ACP) {  
 2     int len1 = MultiByteToWideChar(CP_ACP, 0, wch, -1, NULL, 0);   
 3 
 4     //exchanged
 5     wchar_t * pwtr = new wchar_t[len1+1];  
 6     int len2 = MultiByteToWideChar(CP_ACP, 0, wch, -1, pwtr, len1);  
 7     CStringW str(pwtr);
 8     delete[] pwtr;
 9     return str;  
10 } 
11 
12 CString WCS2UTF8(const wchar_t* szU8)
13 {
14     //get the length of space
15     int len;
16     len = WideCharToMultiByte(CP_UTF8, 0, szU8, -1, NULL, 0, NULL, NULL);
17     char *szUtf8 = new char[len + 1];
18     memset(szUtf8, 0, len + 1);
19     WideCharToMultiByte(CP_UTF8, 0, szU8, -1, szUtf8, len, NULL, NULL);
20     return szUtf8;
21 
22     CString unicodeString(szUtf8);
23 
24     delete[] szUtf8;
25     szUtf8 = NULL;
26     return unicodeString;
27 }

The demo will be like:

原文地址:https://www.cnblogs.com/WaterGood/p/12270916.html