[C/C ++] _ [Unicode (char *) to UTF8]

Source: Internet
Author: User

1. here is only the conversion of a single unicode character. to convert a string, you need to traverse the entire string. You can use the append of std: string to add it. 2. for unicode with a 2-byte width, you only need unicode_char_length = 2. 3. if you have any questions, please point out. [cpp] void OneUnicode2UTF8 (const char * unicode_char, size_t unicode_char_length, char * utf_char) {// unicode: 0x192-> 110010010, utf8: 0xC692-> 1100011010010010 int value = 0; memcpy (& value, unicode_char, unicode_char_length); if (value> = 0x0000 & value <= 0x007F) {utf_char [0] = unicode_char [0];} else if (value> = 0x0080 & value <= 0x07FF) {utf_char [0] = (value> 6) | 0xC0 ); utf_char [1] = (value & 0x3F) | 0x80);} else if (value> = 0x0800 & value <= 0 xFFFF) {utf_char [0] = (value> 12) | 0xE0); utf_char [1] = (value> 6 & 0x3F) | 0x80 ); utf_char [2] = (value & 0x3F) | 0x80);} www.2cto.com else if (value >=0x10000 & value <= 0x10FFFF) {utf_char [0] = (value> 18 | 0xF0); utf_char [1] = (value> 12 & 0x3F) | 0x80 ); utf_char [2] = (value> 6 & 0x3F) | 0x80); utf_char [3] = (value & 0x3F) | 0x80 );} else {cerr <"value too big. "<endl; assert (0 );}}

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.