char*和CString转换

        CString 是一种很特殊的 C++ 对象,它里面包含了三个值:一个指向某个数据缓冲区的指针、一个是该缓冲中有效的字符记数(它是不可存取的,是位于 CString 地址之下的一个隐藏区域)以及一个缓冲区长度。 有效字符数的大小可以是从0到该缓冲最大长度值减1之间的任何数(因为字符串结尾有一个NULL字符)。字符记数和缓冲区长度被巧妙隐藏。

(1) char*转换成CString

  若将char*转换成CString,除了直接赋值外,还可使用CString::Format进行。例如:
  1. char chArray[] = “Char  test”;  
  2. TCHAR * p = _T(“Char  test”);( 或LPTSTR p = _T(“Char  test”);)  
  3. CString theString = chArray;  
  4. theString.Format(_T(”%s”), chArray);  
  5. theString = p;  
         char chArray[] = "Char  test";
         TCHAR * p = _T("Char  test");( 或LPTSTR p = _T("Char  test");)
         CString theString = chArray;
         theString.Format(_T("%s"), chArray);
         theString = p;

(2) CString转换成char*

  若将CString类转换成char*(LPSTR)类型,常常使用下列几种方法:

  方法一,使用强制转换。

  1. CString theString( (_T(“Char test ”));  
  2.  LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;  
       CString theString( (_T("Char test "));
        LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;
   方法二,使用strcpy。
  1. CString theString( (_T(“Char test ”));  
  2. LPTSTR lpsz = new TCHAR[theString.GetLength()+1];  
  3.  _tcscpy(lpsz, theString);  
       CString theString( (_T("Char test "));
       LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
        _tcscpy(lpsz, theString);

  需要说明的是,strcpy(或可移值的_tcscpy)的第二个参数是 const wchar_t* (Unicode)或const char* (ANSI),系统编译器将会自动对其进行转换。

   方法三,使用CString::GetBuffer。
        如果你需要修改 CString 中的内容,它有一个特殊的方法可以使用,那就是 GetBuffer,它的作用是返回一个可写的缓冲指针。 如果你只是打算修改字符或者截短字符串,例如:
  1.  CString s(_T(“Char test ”));  
  2.  LPTSTR p = s.GetBuffer();   
  3.  LPTSTR dot = strchr(p, .);   
  4.  // 在这里添加使用p的代码  
  5. if(p != NULL)   
  6.   *p = _T();  
  7. s.ReleaseBuffer();                     // 使用完后及时释放,以便能使用其它的CString成员函数  
       CString s(_T("Char test "));
       LPTSTR p = s.GetBuffer(); 
       LPTSTR dot = strchr(p, ''.''); 
       // 在这里添加使用p的代码
      if(p != NULL) 
        *p = _T('');
      s.ReleaseBuffer();                     // 使用完后及时释放,以便能使用其它的CString成员函数

    在 GetBuffer 和 ReleaseBuffer 之间这个范围,一定不能使用你要操作的这个缓冲的 CString 对象的任何方法。因为 ReleaseBuffer 被调用之前,该 CString 对象的完整性得不到保障。

        方法四 一个个转换      
  1. int nLength = strSend.GetLength();  
  2. char str[200];  
  3. for(int i= 0; i< nLength; i++)  
  4. {  
  5.     str[i] = strSend[i];  
  6. }  
  7. str[nLength] = ’\0’;  
    int nLength = strSend.GetLength();
    char str[200];
    for(int i= 0; i< nLength; i++)
    {
        str[i] = strSend[i];
    }
    str[nLength] = '\0';
     方法五 用函数
  1. char buf[128];  
  2. int iMinSize = ::WideCharToMultiByte(CP_OEMCP, NULL, strSend.GetBuffer(), -1, NULL, 0, NULL, FALSE);  
  3. ::WideCharToMultiByte(CP_OEMCP, NULL, strSend.GetBuffer(), -1, buf, iMinSize, NULL, FALSE);  
  4. buf[nLength] = ’\0’;  
    char buf[128];
    int iMinSize = ::WideCharToMultiByte(CP_OEMCP, NULL, strSend.GetBuffer(), -1, NULL, 0, NULL, FALSE);
    ::WideCharToMultiByte(CP_OEMCP, NULL, strSend.GetBuffer(), -1, buf, iMinSize, NULL, FALSE);
    buf[nLength] = '\0';

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值