C + + character transcoding

Source: Internet
Author: User

wchar_t* U8tounicode (Char*szU8) {    //UTF8 to Unicode//because the Chinese direct copy will be garbled, the compiler will sometimes error, so the use of 16 binary form//char* szU8 = "abcd1234\xe4\xbd\xa0\xe6\x88\x91\xe4\xbb\x96\x00"; //pre-conversion to get the size of the required space    intWcsLen =:: MultiByteToWideChar (Cp_utf8, NULL, SzU8, strlen (szU8), NULL,0); //allocate space to ' MultiByteToWideChar ' to leave a space, will not give the ' \ ' spacewchar_t* wszstring =NewWchar_t[wcslen +1]; //Conversion:: MultiByteToWideChar (Cp_utf8, NULL, SzU8, strlen (szU8), wszstring, WcsLen); //finally add 'Wszstring[wcslen] =' /'; returnwszstring;}Char* UNICODETOU8 (wchar_t*Szunicode) {    //Unicode to UTF8//wchar_t* wszstring = l "abcd1234 you and Me"; //pre-conversion, to get the size of the space required, this time the function with the above name opposite    intU8len =:: WideCharToMultiByte (Cp_utf8, NULL, Szunicode, Wcslen (Szunicode), NULL,0, NULL, NULL); //as above, allocate space to ' \ ' to leave a space//Although UTF8 is a Unicode form of compression, it is also a multibyte string, so it can be saved as Char    Char* SzU8 =New Char[U8len +1]; //Conversion//the corresponding strlen for the Unicode version is Wcslen:: WideCharToMultiByte (Cp_utf8, NULL, Szunicode, Wcslen (Szunicode), szU8, u8len, NULL, NULL); //finally add 'Szu8[u8len] =' /'; returnszU8;}Char* Unicodetoansi (wchar_t*Szunicode) {    //Unicode to ANSI//wchar_t* wszstring = l "abcd1234 you and Me"; //pre-conversion, to get the size of the space required, this time the function with the above name opposite    intAnsilen =:: WideCharToMultiByte (CP_ACP, NULL, Szunicode, Wcslen (Szunicode), NULL,0, NULL, NULL); //as above, allocate space to ' \ ' to leave a space    Char* Szansi =New Char[Ansilen +1]; //Conversion//the corresponding strlen for the Unicode version is Wcslen:: WideCharToMultiByte (CP_ACP, NULL, Szunicode, Wcslen (Szunicode), Szansi, ansilen, NULL, NULL); //finally add 'Szansi[ansilen] =' /'; returnSzansi;} wchar_t* Ansitounicode (Char*Szansi) {    //ANSI to Unicode//char* szansi = "abcd1234 you i he"; //pre-conversion to get the size of the required space    intWcsLen =:: MultiByteToWideChar (CP_ACP, NULL, Szansi, strlen (Szansi), NULL,0); //allocate space to ' MultiByteToWideChar ' to leave a space, will not give the ' \ ' spacewchar_t* wszstring =NewWchar_t[wcslen +1]; //Conversion:: MultiByteToWideChar (CP_ACP, NULL, Szansi, strlen (Szansi), wszstring, WcsLen); //finally add 'Wszstring[wcslen] =' /'; returnwszstring;}

C + + character transcoding

Related Article

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.