您的位置:首页 > 其它

不同字符集写文件的乱码问题

2017-11-08 11:10 176 查看
1.ANSI与Unicode

Unicode字符集中一个字符占两个字节,GB2312通常采用EUC储存方法,以便兼容于ASCII。每个汉字及符号以两个字节来表示。

2.

const int UNICODE_TXT_FLG = 0xFEFF;

//写入ANSCI

CString strPath;

strPath = L"D://ANSCI_Test.log";

CStringA strTmp;

strTmp =  "测试字符集";

CFile fl1;

fl1.Open(strPath,CFile::modeCreate | CFile::modeReadWrite
| CFile::modeNoTruncate);

int nLen = strTmp.GetLength(); // 10

fl1.Write(StrTmp,strTmp.GetLength());

fl1.Close();

//写入Unicode

strPath = L"D://Unicode_Test.log";

CFile fl2;

//1.TCHAR写入

TCHAR chinfo[] = L"测试字符集";

fl2.Open(strPath,CFile::modeCreate | CFile::modeReadWrite
| CFile::modeNoTruncate);

int nchLen = = wcslen(chinfo); // 5

fl2.Write(UNICODE_TXT_FLG,2);//写入UNICODE头部

fl2.Write(chinfo,nchLen * sizeof(TCHAR));

//2.CString写入

CString strCString;

strCString = _T("CString类的Unicode字符集写入文件");

fl2.Write(strCString,strCString.GetLength() * sizeof(TCHAR));

fl2.Close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  乱码 unicode ascii
相关文章推荐