目录

第1章说明    1

1.1 代码    1

1.2 使用    4

第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);

最新文章

  1. 初玩Linux部署项目
  2. iOS 键盘
  3. 解决ubuntu bash: cd: ~:Permission denied
  4. Black Box 分类: POJ 栈和队列 2015-08-05 14:07 2人阅读 评论(0) 收藏
  5. 最大流算法(Edmons-Karp + Dinic 比较) + Ford-Fulkson 简要证明
  6. linux中nginx的安装,linux的版本是ubutu
  7. ACM第六周竞赛题目——B CodeForces 478B
  8. (转)怎样查看局域网中自己的IP地址和其他电脑的IP地址?
  9. jQuery Moblile Demos学习记录Theming、Button、Icons图标,脑子真的不好使。
  10. 如何实现调用console.log(‘good’.repeat(3))时输出goodgoodgood?
  11. hadoop集群免密码登陆
  12. RabbitMQ消息队列(四):分发到多Consumer(Publish/Subscribe)
  13. MPP 一、Greenplum 集群安装
  14. mysql 中文乱码
  15. PR 审批界面增加显示项方法
  16. Linux 系统进程相关命令
  17. HTML5:定位
  18. Gtk 窗口,控件,设置(添加图片等)
  19. 4、Python文件对象及os、os.path和pickle模块(0530)
  20. laravel使用过程中一些总结

热门文章

  1. jQuery 一些小技巧
  2. QT笔记之模态对话框及非模态对话框
  3. 使用JavaScript输出
  4. 向sql server 导入数据库
  5. MIME协议生成邮件
  6. JAVA 语言基础——运算符
  7. Fragment 与 Fragment 相互传值
  8. GZFramwork快速开发框架演练之会员系统(二)添加字典模块
  9. 【转载】linux内核笔记之高端内存映射
  10. Download file using libcurl in C/C++