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