VC char和TCHAR之间转换
char:計算機編程語言(c、c++、Java、VFP等)中可容納單個字符的一種基本數據類型。
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程序
[cpp] view plaincopy print?示例步驟:
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、啟動結果如下
總結
以上是生活随笔為你收集整理的VC char和TCHAR之间转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: x86、amd64、x86-64、x64
- 下一篇: SQL优化之浅见