【原创】 VC++各种字符串类型转换

 

 

包括VC编程时常用的Unicode和Char互转,CString和std::string互转, 其他的转换可以由这个几个转化间接得到

// convert.h

复制代码
 1  
 2 #include <string>
 3 
 4 #ifndef  _CONVERT_H_
 5 #define  _CONVERT_H_
 6 
 7  
 8 class CConvert
 9 {
10 public:
11     CConvert(void);
12     virtual ~CConvert(void);
13 
14     static int Ansi2Unicode(__in const char* pSrc,  wchar_t *pDst, __in size_t nChsOfDst); 
16 
17     static int Unicode2Ansi(__in const wchar_t * pSrc, char* pDst, __in size_t nBytesOfDst);  
20 static std::string CString2String(CString str); 21 22 23 static CString String2CString(const std::string& str); 24 }; 25 26 #endif
复制代码

 

 //convert.cpp

复制代码
 1 #include "StdAfx.h"
 2 #include "Convert.h"
 3 
 4 
 5 CConvert::CConvert(void)
 6 {
 7 }
 8 
 9 
10 CConvert::~CConvert(void)
11 {
12 }
13 
14 int CConvert::Ansi2Unicode(__in const char* pSrc,  wchar_t *pDst, __in size_t nChsOfDst)
15 { 
16     if (NULL == pDst)
17     {
18         int nChsOfDstNeed = MultiByteToWideChar(CP_ACP, 0, pSrc, -1, NULL, 0); //unicdoe缓冲区需要的字符数,包括\0; 
19         return nChsOfDstNeed;
20     }
21 
22 
23     return MultiByteToWideChar(CP_ACP, 0, pSrc, -1, pDst, nChsOfDst);  
24 }
25 
26 
27 int CConvert::Unicode2Ansi(__in const wchar_t * pSrc, char* pDst, __in size_t nBytesOfDst)
28 {
29     if (NULL == pDst)
30     {
31         int nBytesOfDstNeed = WideCharToMultiByte(CP_ACP, 0, pSrc, -1, NULL, 0, NULL, NULL); //返回需要写的字节数,包括末尾的\0;
32         return nBytesOfDstNeed;
33     }
34 
35     return WideCharToMultiByte(CP_ACP, 0, pSrc, -1, pDst, nBytesOfDst, NULL, NULL); //返回需要写的字节数,包括末尾的\0; 
36 }
37 
38 
39 std::string CConvert::CString2String(CString str)
40 {
41     int len = str.GetLength();
42     TCHAR* pSrc = str.GetBuffer(len); 
43 
44     if (NULL == pSrc)
45     {
46         return NULL;
47     }
48 
49     int nBytesOfDst = Unicode2Ansi(pSrc, NULL, 0);
50     if (0 == nBytesOfDst)
51     {
52         return NULL;
53     }
54 
55     char *pDst = new char[nBytesOfDst / sizeof(char)];
56     memset(pDst, 0, nBytesOfDst);
57 
58     Unicode2Ansi(pSrc, pDst, nBytesOfDst);
59 
60     std::string strRet(pDst);
61     delete[] pDst;
62 
63     return strRet; 
64 }
65 
66 
67 CString CConvert::String2CString(const std::string& str)
68 {
69     CString strRet(str.c_str());
70 
71     return strRet;
72 }
复制代码

 

 

//使用代码实例

复制代码
 1 void CMFCMacrosDlg::Ansi2Unicode()
 2 { 
 3     CConvert obj;
 4     
 5     char* pSrc = "cuihao";
 6     int nChsOfDst = obj.Ansi2Unicode(pSrc, NULL, 0); 
 7     wchar_t *pDst = new wchar_t[nChsOfDst]; 
 8     nChsOfDst = obj.Ansi2Unicode(pSrc, pDst, nChsOfDst);
 9 
10     MessageBox(CString(pDst));
11     
12     delete[] pDst;
13 }
14 
15 
16 void CMFCMacrosDlg::Unicode2Ansi()
17 {
18     // TODO: 在此添加控件通知处理程序代码;
19     CConvert obj;
20 
21     wchar_t *pSrc = L"cuihao";
22 
23     int nBytesOfDst = obj.Unicode2Ansi(pSrc, NULL, 0);
24 
25     char *pDst = new char[nBytesOfDst / sizeof(char)];  //包含\0
26 
27     obj.Unicode2Ansi(pSrc, pDst, nBytesOfDst);
28 
29     MessageBox(CString(pDst));
30 
31     delete[] pDst;
32 }
33 
34 
35 void CMFCMacrosDlg::CString2String()
36 {
37     // TODO: 在此添加控件通知处理程序代码;
38 
39     CString str("cuihao");
40 
41     CConvert obj;
42      
43     std::string strInfo = obj.CString2String(str);
44     
45     MessageBox(obj.String2CString(strInfo));
46 }
47 
48 
49 void CMFCMacrosDlg::String2CString()
50 { 
51     std::string str("cuihao");
52 
53     CConvert obj;
54     CString strRet = obj.String2CString(str); 
55 }
复制代码

 

 

 

 

 

posted on   崔好好  阅读(369)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗

导航

< 2013年4月 >
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 1 2 3 4
5 6 7 8 9 10 11
点击右上角即可分享
微信分享提示