在处理中文时,一个中文通常占用2个字节,而char*和string字符串中的字符都是一个字节的,为了能够有效处理中文,引入了wchar_t*型变量,当需要处理中文时,可以首先将string转换成char*,然后将char*转换成wchar_t*即可。

在window环境中,可以利用

MultiByteToWideChar();
WideCharToMultiByte();

两个API函数来实现转换,方法如下:

单字节字符串string转双字节字符串wchar_t*

#include <windows.h>
#include <string>//不要忘记在使用完wchar_t*后delete[]释放内存
wchar_t *multiByteToWideChar(const string& pKey)
{char* pCStrKey = pKey.c_str();//第一次调用返回转换后的字符串长度,用于确认为wchar_t*开辟多大的内存空间int pSize = MultiByteToWideChar(CP_OEMCP, 0, pCStrKey, strlen(pCStrKey) + 1, NULL, 0); wchar_t *pWCStrKey = new wchar_t[pSize];//第二次调用将单字节字符串转换成双字节字符串MultiByteToWideChar(CP_OEMCP, 0, pCStrKey, strlen(pCStrKey) + 1, pWCStrKey, pSize);return pWCStrKey;
}

双字节字符串wchar_t*转换成单字节字符串char*或string

#include <windows.h>
#include <string>//不要忘记使用完char*后delete[]释放内存
char* wideCharToMultiByte(wchar_t* pWCStrKey)
{//第一次调用确认转换后单字节字符串的长度,用于开辟空间int pSize = WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey), NULL, 0, NULL, NULL);char* pCStrKey = new char[pSize+1];//第二次调用将双字节字符串转换成单字节字符串WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey), pCStrKey, pSize, NULL, NULL);pCStrKey[pSize] = '\0';return pCStrKey;//如果想要转换成string,直接赋值即可//string pKey = pCStrKey;
}

wchar_t*和string相互转换相关推荐

  1. LPCWSTR或LPWSTR与string相互转换

    一.目的: 1.在MFC读取ini配置文件中GetPrivateProfileString获取的是LPWSTR,所以需要将其转换为string 二.操作: 1.MFC读取.ini文件字符串的方法 ht ...

  2. PInvoke复习之深入理解char*与wchar_t*与string以及wstring之间的相互转换

    本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下 #ifndef USE_H_ #define USE_H_ #i ...

  3. golang []byte 和 string相互转换

    原文链接:golang []byte和string相互转换 测试例子 package mainimport ("fmt" )func main() {str2 := "h ...

  4. Python bytes 和 string 相互转换 - Python零基础入门教程

    目录 一.Python bytes 和 string 区别 二.Python string 转 bytes 三. Python bytes 转 string 四.猜你喜欢 基础 Python 学习路线 ...

  5. (C++)wchar_t 转 string / TCHAR转为char/判断是否进程运行/获得目标进程的入口

    首先是wchar_t转string void Wchar_tToString(string& szDst, wchar_t* wchar) {wchar_t* wText = wchar;DW ...

  6. VC之CString,wchar_t,int,string,char*之间的转换

    VC之CString,wchar_t,int,string,char*之间的转换 1. CString 转 wchar_t CString path = "asdf"; wchar ...

  7. wchar_t 转换 string std::string 转换 wchar_t

    测试环境: vs2008 开发语言:C++ #include <iostream> #include <windows.h> #include <string> / ...

  8. cstring转为string vc_VC之CString,wchar_t,int,string,char*之间的转换-阿里云开发者社区

    VC之CString,wchar_t,int,string,char*之间的转换 1. CString 转 wchar_t CString path = "asdf"; wchar ...

  9. Python Bytes和String相互转换,解决Bytes转换String时b‘xxx‘问题

    Python Bytes和String相互转换 发现Python的在bytes数据转string的时候直接用str(xx)会多一个b'xxx'.然后就很麻烦,后来查了下发现bytes和String相互 ...

最新文章

  1. 8、MySQL转义字符的使用
  2. 为了OFFER,花了几个小时,刷下Leetcode链表算法题
  3. CentOS源码编译安装MySQL 5.5.15(转)
  4. draw_circle_mod预生成交互式圆形
  5. MATLAB多元非线性回归
  6. [蓝桥杯2016初赛]煤球数目-找规律
  7. 选择排序 插入排序 交换排序
  8. CSS的七种基本选择器及其权值
  9. Java教程:Java return语句
  10. python中的模块_python3.0中重载模块
  11. 电子科技大学第九届ACM趣味程序设计竞赛(热身赛)题解
  12. Linux命令拆分csv文件,linux – 如何剪切CSV文件
  13. InnoSetup 制作安装包进阶版
  14. 魔兽怀旧服怎么找不到服务器,魔兽世界怀旧服世界服务器无法连接怎么办
  15. 80286计算机配置,电脑的各种配置给详的细解释.doc
  16. 网络计算机自动巡检,网络设备巡检报告.doc
  17. phpcms v9 站内外搜索代码
  18. ResourceExhaustedError
  19. 什么时候需要消息队列
  20. 9 mybatis中使用Java8的日期LocalDate、LocalTime、LocalDateTime

热门文章

  1. 解决方案和项目的区别_沃尔玛用大数据提高销售额,云计算和大数据技术之间的区别汇总...
  2. Java黑皮书课后题第8章:**8.37(猜测首府)编写一个程序,重复提示用户输入一个州的首府。当接收到用户输入后,程序报告答案是否正确。假设50个州以及它们的首府保存在一个二维数组中,提示用户回答所
  3. [BZOJ3932][CQOI2015]任务查询系统(差分+主席树)
  4. 洛谷P2055 [ZJOI2009]假期的宿舍 题解
  5. ImCash:币圈英文术语大全
  6. git 使用详解(3)—— 最基本命令 + .gitignore 文件
  7. Ubuntu下的第一个博客
  8. php数组array_push()和array_pop()以及array_shift()函数
  9. Linux 下安装配置redis
  10. 201621123009 《Java程序设计》第二周学习总结