不同字符集写文件的乱码问题
2017-11-08 11:10
232 查看
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字符集中一个字符占两个字节,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();
相关文章推荐
- 页面文件存储字符集和META指定字符集不同 导致乱码问题
- Java web中不同浏览器间导出Excel文件名称乱码问题解决方案
- FileReader读取文件,由于不确定源文件的编码格式不同,导致读出的文件乱码的问题
- 解决python with 在写入文件是因编码格式不同而造成乱码问题
- 完美转换MySQL的字符集 解决查看utf8源文件中的乱码问题
- Java web中不同浏览器间导出Excel文件名称乱码问题解决方案
- eclipse 不同工程中文乱码问题(可对文件以及工程单独设置)
- FileReader读取文件,由于不确定源文件的编码格式不同,导致读出的文件乱码的问题
- mysql导入导出数据中文乱码解决方法小结(1、navicat导入问题已解决,创建连接后修改连接属性,选择高级->将使用Mysql字符集复选框去掉,下拉框选择GBK->导入sql文件OK;2、phpmyadmin显示乱码的问题也解决,两步:1.将sql文件以utf8的字符集编码另存,2.将文件中sql语句中的字段字符集编码改成utf8,导入OK)
- (好文!!)FileReader读取文件,由于不确定源文件的编码格式不同,导致读出的文件乱码的问题
- 用RAW类型解决不同字符集数据库中文乱码问题
- ubuntu系统下gedit 打开GBK字符集编码文件出现乱码问题解决办法
- (好文!!)FileReader读取文件,由于不确定源文件的编码格式不同,导致读出的文件乱码的问题
- ubuntu系统下gedit 打开GBK字符集编码文件出现乱码问题解决办法
- java下载文件文件名针对不同浏览器乱码,文件名中存在括号等乱码问题
- 针对前台json乱码和不同浏览器文件下载时文件名乱码问题
- 将客户端设置为不同的字符集,演示导入/导出可能产生的乱码问题。为什么没有出现乱码
- 源文件字符集,编译器内部字符集,执行字符集,控制台乱码问题,Qt中文问题
- 解决不同字符集数据库数据传输中文乱码问题
- 使用Unicode字符集时用CFile把中文写入txt文件再用记事本打开出现乱码的问题