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