Convert c/c ++ Chinese strings to Unicode and UTF8

Source: Internet
Author: User

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 ;}

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.