VC char和TCHAR之間轉換

來源:互聯網
上載者:User

char:電腦程式設計語言(c、c++、java、VFP等)中可容納單個字元的一種基礎資料型別 (Elementary Data Type)。

TCHAR:為了滿足Unicode編碼,對char的擴充,即_T(“str”)表示TCHAR類型

C++支援兩種字串,即常規的ANSI編碼(使用""包裹)和Unicode編碼(使用L""包裹),這樣對應的就有了兩套字串字串處理函數,比如:strlen和wcslen,分別用於處理兩種字串char和TCHAR類型

winnt.h標頭檔中:

     typedef WCHAR TCHAR, *PTCHAR; 

表明 TCHAR 與 WCHAR 屬同一類型


char szA[100];                    // ANSI string buffer

WCHAR szW[100];            // Unicode string buffer

// Normal sprintf:all strings are ANSI
sprintf(szA, "%s","ANSI Str");

// Converts Unicode string to ANSI
sprintf(szA,"%S",L"Unicode Str");

// Normal swprintf:all strings are Unicode
swprintf(szW,L"%s",L"Unicode Str");

// Converts ANSI string to Unicode

swprintf(szW,L"%S", "ANSI Str");


注意:大寫S 和小寫s 的使用

===========================


應用執行個體:通過system函數程式調用啟動msc程式

void WSUS::OnBnClickedOk(){CString strPath = NULL;// 申請路徑字串(TCHAR)char strChar[256];// 申請路徑字串(char)m_CustomEdit.GetWindowTextW(strPath);// 擷取路徑儲存到strPathstrPath.Replace(_T("\\"), _T("\\\\"));// 替換strPath中"\"為"\\",注意轉換符//sprintf(strChar, "%s %S", "mmc.exe", strPath);// TCHAR轉換char類型sprintf(strChar, "mmc.exe \"%S\"", strPath);// TCHAR轉換char類型MessageBox(strPath, _T("title"));system(strChar);// 系統函數調用啟動msc程式//WinExec((LPCSTR)_bstr_t(strPath), SW_SHOW);// 調用exe程式}

樣本步驟:

1、擷取msc程式路徑strPath

2、替換strPath中"\"為"\\"字元

C:\Windows\System32\gpedit.msc

首先,通過 strPath.Replace(_T("\\"), _T("\\\\")); 轉換成:

C:\\Windows\\System32\\gpedit.msc

然後,通過 sprintf(strChar, "%s %S", "mmc.exe", strPath); 拼接字串為:

mmc.exe C:\\Windows\\System32\\gpedit.msc

3、system函數調用啟動msc程式
system(strChar);

4、啟動結果如下

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.