VC 中 字符串編程

/*

#ifdef WIDECHAR//#ifdef  /#ifndef 具有在本文件內向上溯源性  在上面的可以識別  在下面的不能識別

#endif

wchar_t *pszText=L"HelloT_char";

wprintf(L"%s\n",pszText);

#else

char *pszText="HelloT_char";

printf("單%s\n",pszText);

#endif

*/

對於wchar_t 類型的字符串 要進行c語言字符串相應的操作 都要加上w


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1.ANSIC與Unicode字符

   兩者區別就不多少了,一個單字節一個雙字節,Unicode可以表示更多的字符,適合諸如漢字的文字系統.

   定義使用寬字符:

 2.聲明Unicode字符與及字符串方法:

   _T()宏需要包含tchar.h

1     wchar_t c=L'A';  
2     wchar_t szBuffer[10]=L"A String";3

WinNt.h定義了一下宏:

複製代碼

  #ifdef UNICODE 
    typedef WCHAR TCHAR ,*PTCHAR,PTSTR;
    typedef CONST WCHAR *PCTSTR;
     __TEXT(quote) quote
     __TEXT(quote) L##quote
 
    
    typedef CHAR TCHAR ,*PTCHAR,PTSTR;
    typedef CONST CHAR *PCTSTR;
     __TEXT(quote) quote
 
    
     __TEXT(quote) quote  TEXT(quote)

複製代碼

 

 可以利用以上宏來定義:在Visual Studio新建工程,一般都開啓了Unicode選項,直接就可以定義寬字符。

複製代碼

1 TCHAR C=TXET('a');//如果定義了Unicode 則是16位字符,否則是8位2     TCHAR C=L'a';3     TCHAR szBuffer[10]=TEXT("a String");4     TCHAR szBuffer[10]=_T("a String");5

複製代碼

3.Ansic與Unicode的轉換:

 

 

 

複製代碼

       *c =;
     std::cout<<c<<std::endl;
           Length = MultiByteToWideChar(CP_ACP,,c,-,NULL,);
     wchar_t *pWideCharStr =  wchar_t[(wchar_t)*Length];
     MultiByteToWideChar(CP_ACP,,c,,pWideCharStr,Length*(wchar_t));
     std::wcout<<<<pWideCharStr<<std::endl;
          Length = WideCharToMultiByte(CP_ACP,,pWideCharStr,-,NULL,,NULL,NULL);
      *pMultiByte =  [Length*()] ;
     WideCharToMultiByte(CP_ACP,,pWideCharStr,-,pMultiByte,Length*(),NULL,NULL);
     std::cout<<<<pMultiByte;
 
     delete [] pWideCharStr;
     delete [] pMultiByte;

複製代碼

 補充:

       size_t wcstombs ( char * mbstr, const wchar_t * wcstr, size_t max );//Convert wide-character string to multibyte string

       size_t mbstowcs ( wchar_t * wcstr, const char * mbstr, size_t max );//Convert multibyte string to wide-character string

      這兩個看就清楚怎麼用了就不寫了 比上面方法要簡單

4.推薦的字符串使用方式

1).開始將文本字符串想像爲字符的數組,而不是char或字節的數組
2).用通用的數據類型如用TCHAR PTSTR表示字符和字符串
3).統一用TEXT _T宏來表示字面量的字符和字符串,不要混用
4).用BYTE和 PBYTE來表示字節、字節指針和數據緩衝區
5).避免使用printf系列函數,尤其不要使用%s來進行Ansic跟Unicode的互相轉換
      應該使用MultiByteToWideChar和WideCharToMutiByte,具體用法參看MSDN

6).推薦從現在開始就使用Unicode字符,理由很簡單,爲了程序代碼的重用性:Com編程模型只支持Unicode   

      微軟.Net技術,還有新的API函數都不支持ANSIC

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 char跟CString轉換、string跟char轉換、string 跟CString轉換 還有BSTR轉換成char*、char*轉換成BSTR、CString轉換成BSTR、BSTR轉換成CString的

 

我們經常寫程序比如文件路徑需要用到一般都是char*類型的變量作爲參數傳遞,有些函數參數卻是string或者CString,造成了經常需要轉換,在這裏我總結了一下:

char跟CString轉換:

 

ContractedBlock.gifExpandedBlockStart.gif代碼

複製代碼

 CString cstr(_T());
wchar_t* wchar = cstr.GetBuffer();
* c1 =  [cstr.GetLength()+]; cstr.ReleaseBuffer();
wcstombs(c1,wchar,cstr.GetLength()+);
std::cout<<c1<<std::endl; 
delete c1;
    
CString theString(  );
LPTSTR lpsz =  TCHAR[theString.GetLength()+];
_tcscpy(lpsz, theString);
* c2 =  [theString.GetLength()+]; wcstombs(c2,lpsz,theString.GetLength()+);
std::cout<<c2<<std::endl;
delete c2;
    
* p2 = ;
wchar_t *p1 =  wchar_t[]; 
mbstowcs(p1,p2,);
std::wcout<<p1<<std::endl;
CString cstr1;
cstr1.Format(_T(),p1);
::MessageBox(NULL,cstr1,NULL,);
delete p1;

複製代碼

 

 

注意:

       CString 是微軟提供的數據類型,其中GetBuffer()函數是重載了ANSIC跟UNICODE的,假如編譯器設置開啓了Unicode

       那它返回的就是寬字符類型,所以就需要wcstombs函數來轉換。比如VC6下默認不支持Unicode,所以用它返回就不需要轉換

string跟char轉換:

 

ContractedBlock.gifExpandedBlockStart.gif代碼

複製代碼

 s1();
* c3 =  [s1.size()+];
strcpy(c3,s1.c_str());    std::cout<<c3<<std::endl;  
delete c3;

* c4 = ;
 s2(c4);
std::cout<<s2<<std::endl;

複製代碼

 

 

 

注意:

      string是標準庫的數據類型,其成員函數c_str()返回的是ANSIC數據類型,所以不需要轉換直接就爲char,假如需要寬字符,再用mbstowcs就可以了

 

 

string 跟CString轉換:

 

ContractedBlock.gifExpandedBlockStart.gif代碼

複製代碼

CString cstr2;
 s3 = ;
* c5 =  [s3.size()+];
wchar_t* c6 =  wchar_t[s3.size()+];
strcpy(c5,s3.c_str());
mbstowcs(c6,c5,s3.size()+);
cstr2.Format(_T(),c6);  ::MessageBox(NULL,cstr2,NULL,);
delete c5;
delete c6;

CString cs1();
* c7 =  [cs1.GetLength()+];
wcstombs(c7,cs1.GetBuffer(),cs1.GetLength()+);
 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*轉換:

 

ContractedBlock.gifExpandedBlockStart.gif代碼

複製代碼

#include  comment(lib, "comsupp.lib")
 _tmain( argc, _TCHAR* argv[])
{
BSTR bstrText = ::SysAllocString(L);
* lpszText2 = _com_util::ConvertBSTRToString(bstrText);
SysFreeString(bstrText); delete[] lpszText2;
 ;
}
_bstr_t b = bstrText;
* lpszText2 = b;

BSTR bstrText = ::SysAllocString(L);
BSTR bstrText = ::SysAllocStringLen(L,);
BSTR bstrText = ::SysAllocStringByteLen(,);
_variant_t strVar();
BSTR bstrText = strVar.bstrVal;
BSTR bstrText = _bstr_t(); 方法四,使用CComBSTR。例如:
BSTR bstrText = CComBSTR();
CComBSTR bstr();
BSTR bstrText = bstr.m_str;
方法五,使用ConvertStringToBSTR。
例如:
* lpszText = ;
BSTR bstrText = _com_util::ConvertStringToBSTR(lpszText);

複製代碼

 

 

 

 

CString與BSTR轉換:

 

ContractedBlock.gifExpandedBlockStart.gif代碼

複製代碼

CString str();
BSTR bstrText = str.AllocSysString();
…
SysFreeString(bstrText); BSTR bstrText = ::SysAllocString(L);
CStringA str;
str.Empty();
str = bstrText; 或
CStringA str(bstrText);

複製代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章