VC字符处理(二)转换(修改)

本文涉及 : char跟CString转换、string跟char转换、string 跟CString转换 还有BSTR转换成char*、char*转换成BSTR、CString转换成BSTR、BSTR转换成CString的

 

我们经常写程序比如文件路径需要用到一般都是char*类型的变量作为参数传递,有些函数参数却是string或者CString,造成了经常需要转换,在这里我总结了一下:

char跟CString转换:

 

代码
   
     
// CString char wchar_t 转换
// CString 转char
// 方法一
CString cstr(_T( " Hello Charm~! " ));
wchar_t
* wchar = cstr.GetBuffer();
char * c1 = new char [cstr.GetLength() + 1 ]; cstr.ReleaseBuffer();
wcstombs(c1,wchar,cstr.GetLength()
+ 1 );
std::cout
<< c1 << std::endl;
delete c1;

// 方法二
CString theString( " Hello CHarm " );
LPTSTR lpsz
= new TCHAR[theString.GetLength() + 1 ];
_tcscpy(lpsz, theString);
char * c2 = new char [theString.GetLength() + 1 ]; // 必须动态分配足够的空间
wcstombs(c2,lpsz,theString.GetLength() + 1 );
std::cout
<< c2 << std::endl;
delete c2;

// char wchar_t转 CString
char * p2 = " Hello CHarm " ;
wchar_t
* p1 = new wchar_t[ 15 ];
mbstowcs(p1,p2,
15 );
std::wcout
<< p1 << std::endl;
CString cstr1;
cstr1.Format(_T(
" %s " ),p1);
::MessageBox(NULL,cstr1,NULL,
0 );
delete p1;

 

 

注意:

       CString 是微软提供的数据类型,其中GetBuffer()函数是重载了ANSIC跟UNICODE的,假如编译器设置开启了Unicode

       那它返回的就是宽字符类型,所以就需要wcstombs函数来转换。比如VC6下默认不支持Unicode,所以用它返回就不需要转换

string跟char转换:

 

代码
   
     
// string char 互换
// string 转 char
string s1( " Hello Charm " );
char * c3 = new char [s1.size() + 1 ];
strcpy(c3,s1.c_str());
// 需要宽字节类型的 再用mbstowcs就可以了
std::cout << c3 << std::endl;
delete c3;

// char 转string
char * c4 = " Hello CHarm " ;
string s2(c4);
std::cout
<< s2 << std::endl;

 

 

 

注意:

      string是标准库的数据类型,其成员函数c_str()返回的是ANSIC数据类型,所以不需要转换直接就为char,假如需要宽字符,再用mbstowcs就可以了

 

 

string 跟CString转换:

 

代码
   
     
// CString string转换
// string 转CString
CString cstr2;
string s3 = " Hello CHarm " ;
char * c5 = new char [s3.size() + 1 ];
wchar_t
* c6 = new wchar_t[s3.size() + 1 ];
strcpy(c5,s3.c_str());
mbstowcs(c6,c5,s3.size()
+ 1 );
cstr2.Format(_T(
" %s " ),c6); // 宽字符环境下CString需要宽字符
::MessageBox(NULL,cstr2,NULL, 0 );
delete c5;
delete c6;

// CString 转string
CString cs1( " Hello Charm " );
char * c7 = new char [cs1.GetLength() + 1 ];
wcstombs(c7,cs1.GetBuffer(),cs1.GetLength()
+ 1 );
string s(c7);
std::cout
<< s << std::endl;
delete c7;

 

 

 

注意:

      由上面两个就可以清楚知道:CString会根据环境重载ANSIC还是UNICODE版本的,而string只能是ANSIC版本的

      而string跟CString转换原理是通过都转换为传统的char类型,然后再转换到对方,因此这里有需要进行ANSIC跟UICODE的转换

      比如string接受传统的string s(char* ); 但CString.GetBuffer()返回有可能是wchar_t*类型的


BSTR转换成char*、char*转换成BSTR、CString转换成BSTR、BSTR转换成CString

(BSTR是COM里面经常会用到的数据类型,可能大家用得较少,我也没逐一进行认真分析了)

BSTR与char*转换:

 

代码
   
     
// .BSTR与char*转换
// BSTR转换成char*
// 方法一,使用ConvertBSTRToString。例如:
#include #pragma comment(lib, "comsupp.lib")
int _tmain( int argc, _TCHAR * argv[])
{
BSTR bstrText
= ::SysAllocString(L " Test " );
char * lpszText2 = _com_util::ConvertBSTRToString(bstrText);
SysFreeString(bstrText);
// 用完释放
delete[] lpszText2;
return 0 ;
}
// 方法二,使用_bstr_t的赋值运算符重载。例如:
_bstr_t b = bstrText;
char * lpszText2 = b;

// char*转换成BSTR
// 方法一,使用SysAllocString等API函数。例如:
BSTR bstrText = ::SysAllocString(L " Test " );
BSTR bstrText
= ::SysAllocStringLen(L " Test " , 4 );
BSTR bstrText
= ::SysAllocStringByteLen( " Test " , 4 );
// 方法二,使用COleVariant或_variant_t。例如:
_variant_t strVar( " This is a test " );
BSTR bstrText
= strVar.bstrVal;
// 方法三,使用_bstr_t,这是一种最简单的方法。例如:
BSTR bstrText = _bstr_t( " This is a test " ); 方法四,使用CComBSTR。例如:
BSTR bstrText
= CComBSTR( " This is a test " );
//
CComBSTR bstr( " This is a test " );
BSTR bstrText
= bstr.m_str;
方法五,使用ConvertStringToBSTR。
例如:
char * lpszText = " Test " ;
BSTR bstrText
= _com_util::ConvertStringToBSTR(lpszText);

 

 

 

 

CString与BSTR转换:

 

代码
   
     
// CString转换成BSTR
// 使用CStringT::AllocSysString来实现。例如:
CString str( " This is a test " );
BSTR bstrText
= str.AllocSysString();

SysFreeString(bstrText);
// 用完释放

// BSTR转换成CString
// 一般可按下列方法进行:
BSTR bstrText = ::SysAllocString(L " Test " );
CStringA str;
str.Empty();
str
= bstrText; 或
CStringA str(bstrText);

 

 

 

/*转载请注明出处:www.gbcdbj..com---charm*/


你可能感兴趣的:(转换)