C + + Chinese string to Unicode and UTF8

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.