1.有时我们编码生成输出的一些内容,通过软件打开可能会出现乱码的现象或者出现格式上的问题需要将其输出转换我utf-8的编码格式。在这里提供一种转换的方法。
BOOL CtreetoxmlDlg::WriteUTF(CFile &file, CString sTxt)
{
LPCSTR pUtf8 = LPCSTR(sTxt);
UINT nLength=strlen(pUtf8);
int nChar = ::MultiByteToWideChar(CP_ACP,0, pUtf8, nLength,NULL, 0);
CStringW tempBuffer;
nChar = ::MultiByteToWideChar(CP_ACP, 0, pUtf8, nLength, (LPWSTR)tempBuffer.GetBufferSetLength(nChar), nChar);
int u8Len = ::WideCharToMultiByte(CP_UTF8, NULL, tempBuffer, wcslen(tempBuffer), NULL, 0, NULL, NULL);
char* szU8 = new char[u8Len + 1];
::WideCharToMultiByte(CP_UTF8, NULL, (LPCWSTR)tempBuffer, wcslen(tempBuffer), szU8, u8Len, NULL, NULL);
szU8[u8Len] = '\0';
file.Write(szU8, u8Len);
return TRUE;
}