用searech and replace查找UFT8文件格式里的中文字符,始终是找不到,木有办法,只好转了。
(这段代码借签了网上某位兄弟的代码,找不到那网页了)
int ConvStrUtf8ToAnsi(CString& strSource, CString& strChAnsi)
{
if (strSource.GetLength() <= 0)
return 0;
CString strWChUnicode;
strSource.TrimLeft();
strSource.TrimRight();
strChAnsi.Empty();
//返回需要strSource转换成宽字符的缓冲的长度
int iLenByWChNeed = MultiByteToWideChar(CP_UTF8, 0,
strSource.GetBuffer(0),
strSource.GetLength(), //MultiByteToWideChar
NULL, 0);
//
int iLenByWchDone = MultiByteToWideChar(CP_UTF8, 0,
strSource.GetBuffer(0),
strSource.GetLength(),
(LPWSTR)strWChUnicode.GetBuffer(iLenByWChNeed * 2),
iLenByWChNeed); //MultiByteToWideChar
strWChUnicode.ReleaseBuffer(iLenByWchDone * 2);
int iLenByChNeed = WideCharToMultiByte(CP_ACP, 0,
(LPCWSTR)strWChUnicode.GetBuffer(0),
iLenByWchDone,
NULL, 0,
NULL, NULL);
int iLenByChDone = WideCharToMultiByte(CP_ACP, 0,
(LPCWSTR)strWChUnicode.GetBuffer(0),
iLenByWchDone,
strChAnsi.GetBuffer(iLenByChNeed),
iLenByChNeed,
NULL, NULL);
strChAnsi.ReleaseBuffer(iLenByChDone);
if (iLenByWChNeed != iLenByChNeed || iLenByWchDone != iLenByChDone)
return 1;
return 0;
}
思路就是先从utf8转到uncode 再从uncode转到ANSI
读取文件到buffer转换后再写入
BOOL TranslateFileToAnsi(char *sFileName)
{
CFile wFile;
wFile.Open(sFileName,CFile::modeReadWrite,NULL);
int iFileLength = wFile.GetLength();
char *sBuf = new char[iFileLength];
//把文件读入缓冲区
wFile.Read(sBuf,iFileLength);
CString str1,str2;
str1.Format("%s",sBuf);
if(ConvStrUtf8ToAnsi(str1,str2) == 1)
{
//转换成功,写入文件,并删除原有文件
wFile.Close();
DeleteFile(sFileName);
int iRetLen = str2.GetLength();
if(iRetLen < 1) return FALSE;
char *sRetBuf = new char[iRetLen];
memcpy(sRetBuf,str2.GetBuffer(iRetLen),iRetLen);
CFile fNewFile;
fNewFile.Open(sFileName,CFile::modeCreate|CFile::modeReadWrite,NULL);
fNewFile.Write(sRetBuf,iRetLen);
fNewFile.Close();
delete sRetBuf;
delete sBuf;
return TRUE;
}
else
{
//关闭文件
delete sBuf;
wFile.Close();
return FALSE;
}
}
功能实现了,但还是有些问题,在转换之前如保判断文件的编码格式?