posts - 62,  comments - 19,  trackbacks - 0
char*转换成CString  
   
  若将char*转换成CString,除了直接赋值外,还可使用CString::format进行。例如:  
  char   chArray[]   =   "This   is   a   test";  
  char   *   p   =   "This   is   a   test";  
   
  或  
   
  LPSTR   p   =   "This   is   a   test";   或在已定义Unicode应的用程序中  
   
  TCHAR   *   p   =   _T("This   is   a   test");   或  
   
  LPTSTR   p   =   _T("This   is   a   test");  
  CString   theString   =   chArray;  
  theString.format(_T("%s"),   chArray);  
  theString   =   p;  
   
  2、CString转换成char*  
   
  若将CString类转换成char*(LPSTR)类型,常常使用下列三种方法:  
   
  方法一,使用强制转换。例如:  
   
  CString   theString(   "This   is   a   test"   );  
  LPTSTR   lpsz   =(LPTSTR)(LPCTSTR)theString;   方法二,使用strcpy。例如:  
   
  CString   theString(   "This   is   a   test"   );  
  LPTSTR   lpsz   =   new   TCHAR[theString.GetLength()+1];  
  _tcscpy(lpsz,   theString);   需要说明的是,strcpy(或可移值Unicode/MBCS的_tcscpy)的第二个参数是   const   wchar_t*   (Unicode)或const   char*   (ANSI),系统编译器将会自动对其进行转换。  
   
  方法三,使用CString::GetBuffer。例如:  
   
  CString   s(_T("This   is   a   test   "));  
  LPTSTR   p   =   s.GetBuffer();  
  //   在这里添加使用p的代码  
  if(p   !=   NULL)   *p   =   _T('\0');  
  s.ReleaseBuffer();   //   使用完后及时释放,以便能使用其它的CString成员函数  
posted on 2007-08-03 17:05 乔栋 阅读(339) 评论(0)  编辑 收藏 引用 所属分类: C++的健身房

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2007年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(6)

随笔分类

随笔档案

文章分类

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜


My blog is worth $0.00.
How much is your blog worth?