【原创】 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 }

 

 

 

 

 

你可能感兴趣的:(【原创】 VC++各种字符串类型转换)