适用于Visual C++的转码函数
acmilan2015/06/11软件综合 IP:四川
由于历史遗留问题,在Visual C++中编码通常是比较令人头疼的问题

程序内码是编译时定义的,Windows的窗口注册时所用的字符编码,一般分为ANSI/MBCS(中文系统是GBK)或Unicode。

GBK - 程序内码之间的转换是非常简单的,因为CString本身就有转码功能。

GBK转换为程序内码
CString str1 = (CString) gbk_str;
程序内码转换为GBK
CStringA gbk_str = (CStringA) str1;

Unicode转换为程序内码
CString str1 = (CString) unicode_str;
程序内码转换为Unicode
CStringW unicode_str = (CStringW) str1;

UTF-8转码则需要使用WinAPI函数MultiByteToWideChar和WideCharToMultiByte,但是使用有点麻烦,可以写两个函数封装转码过程。

使用方法1:UTF-8转换为程序内码
CString str3 = mbcs2tcs(utf8_str, true);
使用方法2:程序内码转换为UTF-8
CStringA utf8_str = tcs2mbcs(str3, true);
<code class="lang-cpp">CString mbcs2tcs(CStringA astr, bool isutf8)
{
    if (isutf8) {
        int length = MultiByteToWideChar(CP_UTF8, 0, astr, -1, NULL, 0);
        wchar_t *bufnew = new wchar_t[length + 1];
        MultiByteToWideChar(CP_UTF8, 0, astr, -1, bufnew, length);
        bufnew[length] = 0;
        CString wstr(bufnew);
        delete []bufnew;
        return wstr;
    }
    else {
        return (CString) astr;
    }
}
 
CStringA tcs2mbcs(CString str, bool isutf8)
{
    if (isutf8) {
        CStringW wstr = (CStringW) str;
        int length = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
        char *bufnew = new char[length + 1];
        WideCharToMultiByte(CP_UTF8, 0, wstr, -1, bufnew, length, NULL, NULL);
        bufnew[length] = 0;
        CStringA astr(bufnew);
        delete []bufnew;
        return astr;
    }
    else {
        return (CStringA) str;
    }
}</code>

[修改于 9年0个月前 - 2015/06/12 09:51:25]

来自:计算机科学 / 软件综合
2
已屏蔽 原因:{{ notice.reason }}已屏蔽
{{notice.noticeContent}}
~~空空如也
acmilan 作者
9年0个月前 修改于 9年0个月前 IP:四川
772821
Unicode编译版本下
CString:
在以CFile::typeText打开的文件,ReadString()、WriteString()读写Unicode文档(typeText将"\n\0"替换为"\r\0\n\0")
在以CFile::typeText打开的文件,Read()、Write()读写MBCS文档(typeText将"\n"替换为"\r\n")
在以CFile::typeBinary打开的文件,ReadString()、WriteString()读写Unicode文档
在以CFile::typeBinary打开的文件,Read()、Write()读写任何文档
CStringA:
可以通过Read()、Write()读写,但是不能通过ReadString()、WriteString()读写。
CStringW:
在以CFile::typeText打开的文件,可以通过ReadString()、WriteString()读写,而不能通过Read()、Write()读写。(换行符会出问题)
在以CFile::typeBinary打开的文件,可以通过ReadString()、WriteString()、Read()、Write()读写。

MBCS编译版本下
CString:
在以CFile::typeText打开的文件,Read()、Write()、ReadString()、WriteString()均读写MBCS文档(typeText将"\n"替换为"\r\n")
在以CFile::typeBinary打开的文件,ReadString()、WriteString()读写MBCS文档
在以CFile::typeBinary打开的文件,Read()、Write()读写任何文档
CStringA:
可以通过Read()、Write()、ReadString()、WriteString()读写。
CStringW:
在以CFile::typeText打开的文件,无法正常读写。
在以CFile::typeBinary打开的文件,可以通过Read()、Write()读写,但是不能通过ReadString()、WriteString()读写。
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论

想参与大家的讨论?现在就 登录 或者 注册

所属专业
上级专业
同级专业
acmilan
进士 学者 笔友
文章
461
回复
2934
学术分
4
2009/05/30注册,5年3个月前活动
暂无简介
主体类型:个人
所属领域:无
认证方式:邮箱
IP归属地:未同步
文件下载
加载中...
{{errorInfo}}
{{downloadWarning}}
你在 {{downloadTime}} 下载过当前文件。
文件名称:{{resource.defaultFile.name}}
下载次数:{{resource.hits}}
上传用户:{{uploader.username}}
所需积分:{{costScores}},{{holdScores}}下载当前附件免费{{description}}
积分不足,去充值
文件已丢失

当前账号的附件下载数量限制如下:
时段 个数
{{f.startingTime}}点 - {{f.endTime}}点 {{f.fileCount}}
视频暂不能访问,请登录试试
仅供内部学术交流或培训使用,请先保存到本地。本内容不代表科创观点,未经原作者同意,请勿转载。
音频暂不能访问,请登录试试
支持的图片格式:jpg, jpeg, png
插入公式
评论控制
加载中...
文号:{{pid}}
投诉或举报
加载中...
{{tip}}
请选择违规类型:
{{reason.type}}

空空如也

加载中...
详情
详情
推送到专栏从专栏移除
设为匿名取消匿名
查看作者
回复
只看作者
加入收藏取消收藏
收藏
取消收藏
折叠回复
置顶取消置顶
评学术分
鼓励
设为精选取消精选
管理提醒
编辑
通过审核
评论控制
退修或删除
历史版本
违规记录
投诉或举报
加入黑名单移除黑名单
查看IP
{{format('YYYY/MM/DD HH:mm:ss', toc)}}