1. Description
Do system programming on Windows, and you will encounter problems dealing with Chinese strings. Most of the time Chinese characters are presented in multibyte-coded ways. For better compatibility or some special needs, such as appearing on a Web page. It is often necessary to convert it into Unicode or UTF8 format.
2. Code example 2.1 Chinese string to Unicode
/*************************************************************************int CN2Unicode(char *input,wchar_t *output)*功能:中文字符转换为unicode字符*参数:input,包含中文的字符串,output,Unicode字符串**************************************************************************/int CN2Unicode(char *input,wchar_t *output){ intstrlen(input); //wchar_t *out = (wchar_t *) malloc(len*sizeof(wchar_t)); len=MultiByteToWideChar(CP_ACP,0,input,-1,output,MAX_PATH); return1;}
2.2 Chinese character string turn UTF8
/*************************************************************************int CN2Utf8 (Char *input,char * Output) * Function: Chinese string converted to UTF8 String * parameter: input, String containing Chinese, Output,utf8 string *************************************************** **********************/intCn2utf8 (Char*input,Char*output) {intLen;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;}
C + + Chinese string to Unicode and UTF8