Unicode下CString和char *之间的互相转换

CString中存储的字符的数据类型为wchar_t类型。

一、CString转换为char *

(1)方法一:使用wcstombs()

[cpp]  view plain copy
  1. #include <iostream>  
  2. using namespace std;  
  3. #include <atlstr.h>  
  4.   
  5. int main()  
  6. {  
  7.     CString str = L"liuxijiao计算机网络";  
  8.     wchar_t *pWChar = str.GetBuffer(); //获取str的宽字符用数组保存  
  9.     str.ReleaseBuffer();  
  10.   
  11.     int nLen = str.GetLength(); //获取str的字符数  
  12.     char *pChar = new char[nLen * 2 + 1];   
  13.     memset(pChar, 0, nLen * 2 + 1);  
  14.     int rtnVal = (int)wcstombs(pChar, pWChar, nLen * 2 + 1); //宽字符转换为多字节字符  
  15.   
  16.     cout<<pChar<<endl;  
  17.   
  18.          delete[] pChar;  
  19.     return 0;  
  20. }  

输出结果:

注意到结果没有输出“计算机网络”,那是因为wcstombs()不支持中文。

(2)方法二:使用WideCharToMultiByte();

[cpp]  view plain copy
  1. #include <iostream>  
  2. using namespace std;  
  3. #include <atlstr.h>  
  4.   
  5. int main()  
  6. {  
  7.     CString str = L"liuxijiao计算机网络";  
  8.     int n = str.GetLength(); //获取str的字符数  
  9.     int len = WideCharToMultiByte(CP_ACP, 0, str, n, NULL, 0, NULL, NULL); //获取宽字节字符的大小,大小是按字节计算的  
  10.     char *pChar = new char[len + 1]; //以字节为单位  
  11.     WideCharToMultiByte(CP_ACP, 0, str, n, pChar, len, NULL, NULL); //宽字节编码转换成多字节编码  
  12.     pChar[len + 1] = '\0'//多字节字符以'\0'结束  
  13.     cout<<pChar<<endl;  
  14.          delete[] pChar;  
  15.     return 0;  
  16. }  


输出结果:


(3)方法三:T2A()宏:

CString cs_test = _T("test中国");
char *psz_appid = T2A(cs_test);


二、char *转换为CString

(1)方法一:使用_T()宏

[cpp]  view plain copy
  1. CString str = _T("liuxijiao计算机网络");  

(2)方法二:使用API的函数MultiByteToWideChar()

[cpp]  view plain copy
  1. #include <iostream>  
  2. using namespace std;  
  3. #include <atlstr.h>  
  4. #include <stdio.h>  
  5. #include <string.h>  
  6.   
  7. int main()  
  8. {  
  9.     //将char数组转换为wchar_t数组  
  10.     char *pChar = "liuxijiao计算机网络";  
  11.     int charLen = strlen(pChar); //计算pChar所指向的字符串大小,以字节为单位,一个汉字占两个字节  
  12.     int len = MultiByteToWideChar(CP_ACP, 0, pChar, charLen, NULL, 0); //计算多字节字符的大小,按字符计算  
  13.     wchar_t *pWChar = new wchar_t[len + 1]; //为宽字节字符数申请空间,  
  14.     MultiByteToWideChar(CP_ACP, 0, pChar, charLen, pWChar, len); //多字节编码转换成宽字节编码  
  15.     pWChar[len] = '\0';  
  16.   
  17.     //将wchar_t数组转换为CString  
  18.     CString str;  
  19.     str.Append(pWChar);  
  20.   
  21.     delete[] pChar;  
  22.     delete[] pWChar;  
  23.     return 0;  
  24. }  

在str.Append(pWChar);这条语句处设断点,调试运行,可查看到str的内容为"liuxijiao计算机网络"。

(三)方法三:使用A2T()、A2W()

[cpp]  view plain copy
  1. char *pChar = "liuxijiao计算机网络";  
  2. USES_CONVERSION;  
  3. CString str = A2T(pChar);  
[cpp]  view plain copy
  1. char *pChar = "liuxijiao计算机网络";  
  2. USES_CONVERSION;  
  3. CString str = A2W(pChar);  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值