CString 和 char * 的相互转换

_T的作用
_T和_TEXT是一样的,一般情况_T("Kurt")和“Kurt"没有什么区别。但对于UNICODE,一般加上_T.
如果想编译UNICODE版本的软件,则加上_T可以由编译器自动把字符串转换为Unicode的。
 
char* str,或unsigned char* str转换成CString

1。直接用构造函数。 
CString( LPCTSTR lpch, int nLength ); 
CString( const unsigned char* psz );

例:char ch[] = _T("this is a sample."); 
      CString str(ch); //or CString str = ch;

2用Format函数

例: char* test="asfdasfd"; 
     CString str; 
     str.Format("%s", test);

3强制转换

   (CString)char强制转换  

CString 转换为char *

LPTSTR 和char *意思同

1使用强制转换

例如:
CString theString( "This is a test" );
LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;

2使用CString::GetBuffer

CString s(_T("This is a test "));
LPTSTR p = s.GetBuffer();
// 在这里添加使用p的代码
if(p != NULL) *p = _T('\0');
s.ReleaseBuffer(); 
// 使用完后及时释放,以便能使用其它的CString成员函数。

本文转自博客园知识天地的博客,原文链接:CString 和 char * 的相互转换,如需转载请自行联系原博主。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值