首页 > 代码库 > 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换 [转]

深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换 [转]

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

 1   #ifndef USE_H_   2     #define USE_H_   3  4     #include <iostream>   5     #include <windows.h>   6     #include <string>   7     using namespace std;   8     class CUser   9     {  10     public:  11         CUser();  12         virtual~ CUser();  13         char* WcharToChar(const wchar_t* wp);  14         char* StringToChar(const string& s);  15         char* WstringToChar(const wstring& ws);  16         wchar_t* CharToWchar(const char* c);  17         wchar_t* WstringToWchar(const wstring& ws);  18         wchar_t* StringToWchar(const string& s);  19         wstring StringToWstring(const string& s);  20         string WstringToString(const wstring& ws);  21         void Release();  22     private:  23         char* m_char;  24         wchar_t* m_wchar;  25     };  26     #endif; 

 

 1     #include "stdafx.h"   2     #include "use.h"   3     CUser::CUser()   4     :m_char(NULL)   5     ,m_wchar(NULL)   6     {   7     }   8     CUser::~CUser()   9     {  10         Release();  11     }  12     char* CUser::WcharToChar(const wchar_t* wp)  13     {  14         Release();  15         int len= WideCharToMultiByte(CP_ACP,0,wp,wcslen(wp),NULL,0,NULL,NULL);  16         m_char=new char[len+1];  17         WideCharToMultiByte(CP_ACP,0,wp,wcslen(wp),m_char,len,NULL,NULL); 18         m_char[len]=\0;  19         return m_char;  20     }  21     wchar_t* CUser::CharToWchar(const char* c)  22     {  23         Release();  24         int len = MultiByteToWideChar(CP_ACP,0,c,strlen(c),NULL,0);  25         m_wchar=new wchar_t[len+1];  26         MultiByteToWideChar(CP_ACP,0,c,strlen(c),m_wchar,len);  27         m_wchar[len]=\0;  28         return m_wchar;  29     }  30     void CUser::Release()  31     {  32         if(m_char)  33         {  34             delete m_char;  35             m_char=NULL;  36         }  37         if(m_wchar)  38         {  39             delete m_wchar;  40             m_wchar=NULL;  41         }  42     }  43     char* CUser::StringToChar(const string& s)  44     {  45         return const_cast<char*>(s.c_str());  46     }  47     char* CUser::WstringToChar(const std::wstring &ws)  48     {  49         const wchar_t* wp=ws.c_str();  50         return WcharToChar(wp);  51     }  52     wchar_t* CUser::WstringToWchar(const std::wstring &ws)  53     {  54         return const_cast<wchar_t*>(ws.c_str());  55     }  56     wchar_t* CUser::StringToWchar(const string& s)  57     {  58         const char* p=s.c_str();  59         return CharToWchar(p);  60     }  61     string CUser::WstringToString(const std::wstring &ws)  62     {  63         string s;  64         char* p=WstringToChar(ws);  65         s.append(p);  66         return s;  67     }  68     wstring CUser::StringToWstring(const std::string &s)  69     {  70         wstring ws;  71         wchar_t* wp=StringToWchar(s);  72         ws.append(wp);  73         return ws;  74     }  

 

如果涉及到CString,可以利用CString::GetBuffer()函数来进行转换,道理一样.

 

转自:http://www.jb51.net/article/37627.htm