VC++ 中使用 std::string 转换字符串编码
目录
第1章说明
VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量。
1.1 代码
函数声明如下:
std::string stringA2W(const char* pA,int nA,UINT uCodePage = CP_ACP); std::string stringW2A(const wchar_t*pW,int nW,UINT uCodePage = CP_ACP); std::string stringA2W(const std::string&sA,UINT uCodePage = CP_ACP); std::string stringW2A(const std::string&sW,UINT uCodePage = CP_ACP); |
函数实现如下:
/***************************************************************\ 窄字符串 ==> 宽字符串(UTF16LE) pA [in] 窄字符串首地址 nA [in] 窄字符串长度。小于零就使用 strlen 计算长度。 uCodePage [in] 窄字符串的代码页 如:CP_ACP 表示系统默认;936 表示 GBK…… 返回: 宽字符串 sW 宽字符串的字符数 nChar = sW.length() / sizeof(wchar_t) 宽字符串的字节数 nByte = (nChar + 1) * sizeof(wchar_t) - 1 字节数多加了 sizeof(wchar_t) - 1 = 1 个 \0,是为了下面的用法 const wchar_t* pW = (const wchar_t*)sW.c_str(); \***************************************************************/ std::string stringA2W(const char*pA,int nA,UINT uCodePage) { std::string sW; if(pA) { if(nA < 0) { nA = strlen(pA); } if(nA > 0) { int nW = MultiByteToWideChar(uCodePage,0,pA,nA,NULL,0); if(nW > 0) { int nByte = (nW + 1) * sizeof(wchar_t); wchar_t*pW = (wchar_t*)malloc(nByte); if(pW) { MultiByteToWideChar(uCodePage,0,pA,nA,pW,nW); pW[nW] = L'\0'; sW.assign((const char*)pW,nByte - 1); free(pW); } } } } if(sW.empty()) { sW = std::string(sizeof(wchar_t) - 1,'\0'); } return sW; } /***************************************************************\ 窄字符串 ==> 宽字符串(UTF16LE) sA [in] 窄字符串 uCodePage [in] 窄字符串的代码页 如:CP_ACP 表示系统默认;936 表示 GBK…… 返回:宽字符串 \***************************************************************/ std::string stringA2W(const std::string&sA,UINT uCodePage) { return stringA2W(sA.c_str(),sA.length(),uCodePage); } /***************************************************************\ 宽字符串(UTF16LE) ==> 窄字符串 pW [in] 宽字符串首地址 nW [in] 宽字符串字符数。小于零就使用 wcslen 计算长度。 uCodePage [in] 窄字符串的代码页 如:CP_ACP 表示系统默认;936 表示 GBK…… 返回:窄字符串 \***************************************************************/ std::string stringW2A(const wchar_t*pW,int nW,UINT uCodePage) { std::string sA; if(pW) { if(nW < 0) { nW = wcslen(pW); } if(nW > 0) { int nA = WideCharToMultiByte(uCodePage,0,pW,nW ,NULL,NULL,NULL,NULL); if(nA > 0) { char*pA = (char*)malloc(nA); if(pA) { WideCharToMultiByte(uCodePage,0,pW,nW ,pA,nA,NULL,NULL); sA.assign(pA,nA); free(pA); } } } } return sA; } /***************************************************************\ 宽字符串(UTF16LE) ==> 窄字符串 sW [in] 宽字符串,编码为 UTF16LE uCodePage [in] 窄字符串的代码页 如:CP_ACP 表示系统默认;936 表示 GBK…… 返回:窄字符串 \***************************************************************/ std::string stringW2A(const std::string&sW,UINT uCodePage) { return stringW2A((const wchar_t*)sW.c_str() ,sW.length() / sizeof(wchar_t),uCodePage); } |
1.2 使用
有了上述四个函数,字符串的编码转换用一、两行代码即可实现。
如:将GBK字符串"测试"转换为宽字符串
std::string sW = stringA2W("测试"); |
//简体中文 Windows 下 |
std::string sW = stringA2W("测试",936); |
//安装有代码页936的Windows |
如:将GBK字符串"测试"转换为UTF-8编码
std::string sUTF8 = stringW2A(stringA2W("测试",936),CP_UTF8); |
如:将GBK字符串"测试"转换为Big5编码
std::string sBig5 = stringW2A(stringA2W("测试",936),950); |
最新文章
- 初玩Linux部署项目
- iOS 键盘
- 解决ubuntu bash: cd: ~:Permission denied
- Black Box 分类: POJ 栈和队列 2015-08-05 14:07 2人阅读 评论(0) 收藏
- 最大流算法(Edmons-Karp + Dinic 比较) + Ford-Fulkson 简要证明
- linux中nginx的安装,linux的版本是ubutu
- ACM第六周竞赛题目——B CodeForces 478B
- (转)怎样查看局域网中自己的IP地址和其他电脑的IP地址?
- jQuery Moblile Demos学习记录Theming、Button、Icons图标,脑子真的不好使。
- 如何实现调用console.log(‘good’.repeat(3))时输出goodgoodgood?
- hadoop集群免密码登陆
- RabbitMQ消息队列(四):分发到多Consumer(Publish/Subscribe)
- MPP 一、Greenplum 集群安装
- mysql 中文乱码
- PR 审批界面增加显示项方法
- Linux 系统进程相关命令
- HTML5:定位
- Gtk 窗口,控件,设置(添加图片等)
- 4、Python文件对象及os、os.path和pickle模块(0530)
- laravel使用过程中一些总结