Unicode to utf8

Source: Internet
Author: User

Specifically, the Unicode encoding here refers to ucs2 encoding. The characters stored in the wchar_t array used by Windows applications should be ucs2 encoding, it is easy to mistakenly think that Unicode is encoded in two bytes. In fact, Unicode represents a character set, that is, the character encoding scheme. It only specifies the binary code of the symbol, but does not specify how the binary code should be stored on the computer, while utf8, UTF16 and ucs2 are various Unicode implementation methods, which define the representation of Unicode binary code on a computer. In addition, the ucs2 encoding does not fully represent the Unicode Character Set.

The encoding method from ucs2 to UTF-8 is as follows:

/* Parameter: strunicode: Unicode string pointer strunicodelen: Unicode String Length strutf8: utf8 string pointer strutf8len: utf8 string bytes. To obtain the number of bytes required for conversion, you can input-1 to this value. returned value: the number of UTF-8 string bytes after conversion.-1 indicates conversion failure */INT unicodetoutf_8 (wchar_t * strunicode, int strunicodelen, char * strutf8, int strutf8len) {If (strunicode = NULL) | (strunicodelen <= 0) | (strutf8len <= 0 & strutf8len! =-1) {return-1;} int I, offset = 0; If (strutf8len =-1) {for (I = 0; I <strunicodelen; I ++) {If (strunicode [I] <= 0x007f) // single-byte encoding {Offset + = 1 ;} else if (strunicode [I]> = 0x0080 & strunicode [I] <= 0x07ff) // Double Byte encoding {offset ++ = 2 ;} else if (strunicode [I]> = 0x0800 & strunicode [I] <= 0 xFFFF) // three-byte encoding {offset ++ = 3 ;}} return Offset + 1 ;}else {If (strutf8 = NULL) {return-1 ;}for (I = 0; I <strunicodelen; I ++) {If (strunicode [I] <= 0x007f) // single-byte encoding {strutf8 [offset ++] = (char) (strunicode [I] & 0x007f );} else if (strunicode [I]> = 0x0080 & strunicode [I] <= 0x07ff) // Double Byte encoding {strutf8 [offset ++] = (char) (strunicode [I] & 0x07c0)> 6) | 0x00c0); strutf8 [offset ++] = (char) (strunicode [I] & 0x003f) | 0x0080);} else if (strunicode [I]> = 0x0800 & strunicode [I] <= 0 xFFFF) // three-byte encoding {strutf8 [offset ++] = (char) (strunicode [I] & 0xf000)> 12) | 0x00e0 ); strutf8 [offset ++] = (char) (strunicode [I] & 0x0fc0)> 6) | 0x0080); strutf8 [offset ++] = (char) (strunicode [I] & 0x003f) | 0x0080) ;}} strutf8 [offset] = '\ 0'; return Offset + 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.