Unicode下CString和char *之间的互相转换
来源:互联网 发布:java架构师考证 编辑:程序博客网 时间:2024/05/18 01:23
CString中存储的字符的数据类型为wchar_t类型。
一、CString转换为char *
(1)方法一:使用wcstombs()
#include <iostream> using namespace std; #include <atlstr.h> int main() { CString str = L"liuxijiao计算机网络"; wchar_t *pWChar = str.GetBuffer(); //获取str的宽字符用数组保存 str.ReleaseBuffer(); int nLen = str.GetLength(); //获取str的字符数 char *pChar = new char[nLen * 2 + 1]; memset(pChar, 0, nLen * 2 + 1); int rtnVal = (int)wcstombs(pChar, pWChar, nLen * 2 + 1); //宽字符转换为多字节字符 cout<<pChar<<endl; delete[] pChar; return 0;输出结果:
liuxijiao
注意到结果没有输出“计算机网络”,那是因为wcstombs()不支持中文。
(2)方法二:使用WideCharToMultiByte();
#include <iostream> using namespace std; #include <atlstr.h> int main() { CString str = L"liuxijiao计算机网络"; int n = str.GetLength(); //获取str的字符数 int len = WideCharToMultiByte(CP_ACP, 0, str, n, NULL, 0, NULL, NULL); //获取宽字节字符的大小,大小是按字节计算的 char *pChar = new char[len + 1]; //以字节为单位 WideCharToMultiByte(CP_ACP, 0, str, n, pChar, len, NULL, NULL); //宽字节编码转换成多字节编码 pChar[len + 1] = '\0'; //多字节字符以'\0'结束 cout<<pChar<<endl; delete[] pChar; return 0; }
输出结果:
liuxijiao计算机网络
二、char *转换为CString
(1)方法一:使用_T()宏
CString str = _T("liuxijiao计算机网络");(2)方法二:使用API的函数MultiByteToWideChar()
#include <iostream> using namespace std; #include <atlstr.h> #include <stdio.h> #include <string.h> int main() { //将char数组转换为wchar_t数组 char *pChar = "liuxijiao计算机网络"; int charLen = strlen(pChar); //计算pChar所指向的字符串大小,以字节为单位,一个汉字占两个字节 int len = MultiByteToWideChar(CP_ACP, 0, pChar, charLen, NULL, 0); //计算多字节字符的大小,按字符计算 wchar_t *pWChar = new wchar_t[len + 1]; //为宽字节字符数申请空间, MultiByteToWideChar(CP_ACP, 0, pChar, charLen, pWChar, len); //多字节编码转换成宽字节编码 pWChar[len] = '\0'; //将wchar_t数组转换为CString CString str; str.Append(pWChar); delete[] pChar; delete[] pWChar; return 0; }
在str.Append(pWChar);这条语句处设断点,调试运行,可查看到str的内容为"liuxijiao计算机网络"。
(三)方法三:使用A2T()、A2W()
char *pChar = "liuxijiao计算机网络"; USES_CONVERSION; CString str = A2T(pChar);
char *pChar = "liuxijiao计算机网络"; USES_CONVERSION; CString str = A2W(pChar);
本文转自:http://blog.csdn.net/candyliuxj/article/details/6796001
0 0
- Unicode下CString和char *之间的互相转换
- Unicode下CString和char *之间的互相转换
- Unicode下CString和char *之间的互相转换
- Unicode下CString和char *之间的互相转换
- Unicode下CString和char *之间的互相转换
- Unicode下CString和char *之间的互相转换
- Unicode下CString和char *之间的互相转换
- 【MFC】 Unicode下CString和char *之间的互相转换
- Unicode下CString和char *之间的互相转换
- Unicode下CString和char *之间的互相转换
- Unicode下CString和char *之间的互相转换
- Unicode下CString和char *之间的互相转换
- CString和char *之间的互相转换
- Unicode下CString与char *之间的转换
- Unicode下CString与char*之间的转换 (VS2008)
- Unicode下CString与char*之间的转换 (VS2008)
- BSTR、CString、char*之间的互相转换
- CString转char * ,string |wchar_t*和char*之间互相转换
- 解决eclipse+tomcat开发时候修改配置文件自动重启服务器问题
- 第三周项目3 多文件组织
- 编译 GCC 4.8.2
- Linux中处理文件名中含有空格的问题
- (2.1.7)Java之Exception和error
- Unicode下CString和char *之间的互相转换
- ARM平台Linux内核内存空间分布
- 黑马程序员_关于javaSE的一些基础知识(2)
- iOS截屏代码
- PHP json_decode返回null解析失败原因
- [省选前衡八题目整理][BZOJ 3223]文艺平衡树(Splay)
- Windows Oracle ODBC的安装配置
- iOS git版本控制的使用(终端),常用指令使用
- [网络编程]_[初级]_[Windows网络编程之搭建服务器和客户端]