指定输出文本为utf-8编码格式

来源:互联网 发布:故宫文创和故宫淘宝 编辑:程序博客网 时间:2024/05/17 04:00

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


原创粉丝点击