Character encoding conversion under Windows

Source: Internet
Author: User

Idea: The input string must end with ' + ' and if the input string does not end with '/', set it manually, otherwise the conversion will have an error.

unsigned int encodeutil::ansitoucs2 (char* pansi, wchar_t*& pUcs2)
{
//Pre-conversion to get the size of the required space
int wcsLen =:: MultiByteToWideChar (CP_ACP, NULL, Pansi, strlen (Pansi), NULL, 0);
//Allocate space to make a space for ' MultiByteToWideChar ', it will not give ' \ ' space
PUCS2 = new Wchar_t[wcslen + 1];
//Conversion
:: MultiByteToWideChar (CP_ACP, NULL, Pansi, strlen (Pansi), PUCS2, wcsLen);
//Last Plus ' /'
Pucs2[wcslen] = ' + ';
return wcsLen;
}

unsigned int encodeutil::ucs2toansi (wchar_t* pUcs2, char*& pansi)
{
//Pre-conversion, to get the size of the required space, this time with the function and the above name opposite
int ansilen =:: WideCharToMultiByte (CP_ACP, NULL, PUCS2, Wcslen (PUCS2), NULL, 0, NULL, NULL);
///Ibid., allocate space to make a space for '/ '
Pansi = new Char[ansilen + 1];
//Conversion
the//unicode version corresponds to the strlen is Wcslen
:: WideCharToMultiByte (CP_ACP, NULL, PUCS2, Wcslen (PUCS2), Pansi, ansilen, NULL, NULL);
//Last Plus ' /'
Pansi[ansilen] = ' + ';
return ansilen;
}

unsigned int Encodeutil::ucs2toutf8 (wchar_t* pUcs2, char*& PUtf8)
{
//Pre-conversion, to get the size of the required space, this time with the function and the above name opposite
int u8len =:: WideCharToMultiByte (Cp_utf8, NULL, PUCS2, Wcslen (PUCS2), NULL, 0, NULL, NULL);
///Ibid., allocate space to make a space for '/ '
Although//utf8 is a Unicode form of compression, it is also a multibyte string, so it can be saved as Char
PUtf8 = new Char[u8len + 1];
//Conversion
the//unicode version corresponds to the strlen is Wcslen
:: WideCharToMultiByte (Cp_utf8, NULL, PUCS2, Wcslen (PUCS2), PUtf8, u8len, NULL, NULL);
//Last Plus ' /'
Putf8[u8len] = ' + ';
return u8len;
}

unsigned int encodeutil::utf8toucs2 (char* PUtf8, wchar_t*& pUcs2)
{
//Pre-conversion to get the size of the required space
int wcsLen =:: MultiByteToWideChar (Cp_utf8, NULL, PUtf8, strlen (PUTF8), NULL, 0);
//Allocate space to make a space for ' MultiByteToWideChar ', it will not give ' \ ' space
PUCS2 = new Wchar_t[wcslen + 1];
//Conversion
:: MultiByteToWideChar (Cp_utf8, NULL, PUtf8, strlen (PUtf8), PUCS2, wcsLen);
//Last Plus ' /'
Pucs2[wcslen] = ' + ';
return wcsLen;
}

unsigned int Encodeutil::ansitoutf8 (char* pansi, char*& PUtf8)
{
wchar_t* PUCS2 = NULL;
ANSITOUCS2 (Pansi, PUCS2);
unsigned int len = Ucs2toutf8 (PUCS2, PUtf8);
Delete[] PUCS2;
return Len;
}

Character encoding conversion under Windows

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.