MFC project using Unicode encoding, reading text data garbled conversion function

Source: Internet
Author: User

When MFC projects use Unicode, cstdiofile.readstring reads that the data is Unicode encoded and must be converted or garbled

Convert byte data to Unicode encoding

int Chartounicode (char *pchin, CString *pstrout)
{
int Nlen;
WCHAR *ptch;

if (Pchin = = NULL)
{
return 0;
}

Nlen = MultiByteToWideChar (CP_ACP, 0, Pchin,-1, NULL, 0);//CP_ACP is the local machine native property

Can be modified into the specified language GB2312 936. Shift-jis 932 UTF-8 65001.
Ptch = new Wchar[nlen];
MultiByteToWideChar (CP_ACP, 0, Pchin,-1, Ptch, Nlen);
Pstrout->format (_t ("%s"), Ptch);

delete [] ptch;

return nlen;
}

Implementation of CString coding changes. Make it possible to display the cost of language.

void function (CString &str)
{
Char *szbuf = new Char[str. GetLength () +1];

int i;
for (i = 0; i < str. GetLength (); i++)
{
Szbuf[i] = str. GetAt (i);
}
Szbuf[i]= '/0 ';
Chartounicode (Szbuf, &STR);

delete []szbuf;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //
Convert Unicode characters to char characters
int Unicodetochar (CString &strin, char *pchout, int ncharlen)
{
if (pchout = = NULL)
{
return 0;
}

int nlen = WideCharToMultiByte (CP_ACP, 0, (LPCWSTR) Strin.getbuffer (Buffer_size_kilo), -1, NULL, 0, NULL, NULL);
Nlen = min (Nlen, Ncharlen);
WideCharToMultiByte (CP_ACP, 0, (LPCWSTR) Strin.getbuffer (Buffer_size_kilo),-1, Pchout,
Nlen, NULL, NULL);

if (Nlen < Ncharlen)
{
Pchout[nlen] = 0;
}

return nlen;
}

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.