Convert c/c ++ Chinese strings to Unicode and UTF8
1. Description
If you do system programming on windows, you will inevitably encounter the problem of Processing Chinese strings. Most of the time, Chinese characters are displayed in Multi-byte encoding. To achieve better compatibility or some special requirements (such as displaying on a webpage .) It is often necessary to convert it to unicode or utf8 format.
2. Sample Code 2.1 convert a Chinese string to Unicode
/*************************************** * ********************************** Int CN2Unicode (char * input, wchar_t * output) * function: converts a Chinese character to a unicode Character * parameter: input, a Chinese character string, output, unicode string ************************************** * **********************************/int CN2Unicode (char * input, wchar_t * output) {int len = strlen (input); // wchar_t * out = (wchar_t *) malloc (len * sizeof (wchar_t); len = MultiByteToWideChar (CP_ACP, 0, input,-1, output, MAX_PATH); return 1 ;}
2.2 convert a Chinese string to utf8
/*************************************** * ********************************* Int CN2Utf8 (char * input, char * output) * function: converts a Chinese string to a utf8 string * parameter: input, containing a Chinese string, output, utf8 string ************************************** * *********************************/int CN2Utf8 (char * input, char * output) {int len; wchar_t * out = (wchar_t *) malloc (len * sizeof (wchar_t); len = MultiByteToWideChar (CP_ACP, 0, input,-1, out, strlen (input) + 1); WideCharToMultiByte (CP_UTF8, 0, out, wcslen (out), output, len, NULL, NULL); return 1 ;}