char和TCHAR之間轉換

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]
void WSUS::OnBnClickedOk() 

    CString strPath = NULL;     // 申請路徑字符串(TCHAR) 
    char strChar[256];  // 申請路徑字符串(char)
    m_CustomEdit.GetWindowTextW(strPath);   // 獲取路徑存儲到strPath  
    strPath.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);


'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'

'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR' – VBForums

In this article the author writes:

You have three valid options. All others are invalid. The third of these options is the best. 
Option 1: Force ANSI (slower on NT, doesn't work on CE, possible problems with internationalization)

Code:

?
1
2
hFile = CreateFileA("C:\\tab.doc",GENERIC_READ|GENERIC_WRITE,
 FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

Option 2: Force Unicode (doesn't work on 9x)

Code:

?
1
2
hFile = CreateFileW(L"C:\\tab.doc",GENERIC_READ|GENERIC_WRITE,
 FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

Option 3: Make it easily switchable in the project settings (easy to get working and ideal versions for everywhere)

Code:

?
1
2
hFile = CreateFile(TEXT("C:\\tab.doc"),GENERIC_READ|GENERIC_WRITE,
 FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

If you include <tchar.h>, you can use _TEXT() or _T() instead of TEXT(), but that is the only permitted variation. Everything else is invalid.

 

OTHER SAY:

Now go into the project settings and change using Unicode to multi-byte character set and see if it still compiles.

    其實主要的問題是vc2008 字符集的問題,其中默認的[字符集]爲 “使用Unicode 字符集“,所以把他改爲 “使用多字節字符集”就可以了。

    設置:單欄裏,點“項目”,然後選“屬性”(最後一項),再點“配置屬性”,是個“+”號,把它展開,然後選“常規”選項卡,倒數第三項“字符集”,選擇你想用的吧。

  網上有人的建議:

不過還是建議使用Unicode,否則你的程序將有很大的侷限性;要是你的程序只在西方發佈還好,但畢竟是適應中國大陸的程序吧,所以建議使用Unicode。LPSTR LPCSTR LPWSTR LPCWSTR區別

LPSTR 一個32位的指向字符串的指針 
LPCSTR 一個32位的指向字符串常量的指針 
LPWSTR 一個32位的指向unicode字符串的指針 
LPCWSTR 一個32位的指向unicode字符串常量的指針 
前面的L代表LONG,P就是指針的意思,C就是constant的意思 
W是wide的意思,STR就是string的意思

LPSTR = char * 
LPCSTR = const char * 
LPWSTR = wchar_t * 
LPCWSTR = const wchar_t * 
LPOLESTR = OLECHAR * = BSTR = LPWSTR(Win32) 
LPCOLESTR = const OLECHAR * = LPCWSTR(Win32) 
LPTSTR = _TCHAR * 
LPCTSTR = const _TCHAR *

多字節字符串與寬字符串的轉換可使用C API者Win32 API. 
C API: mbstowcs,wcstombs 
Win32 API: MultiByteToWideChar, WideCharToMultiByte

下面着重介紹Win32 API的用法,C API的用法較爲簡單可參照Win32 API。

首先是WideCharToMultiByte

通常你需要配置4個參數(其他參數如是使用即可),紅色標記的部分。 
依次是源寬字符串,需要轉換的長度(-1,則爲轉換整個字符串),目標多字節字符串,目標緩衝區長度。 
返回值表示轉換爲目標多字節字符串實際需要的長度(包括結束符)。 
所以通常需要調用WideCharToMultiByte兩次:第一次產生目標緩衝區長度,第二次產生目標字符串,像下面這樣 
wchar_t* wcs = L"中國,你好!I Love You!"; 
int lengthOfMbs = WideCharToMultiByte( CP_ACP, 0, wcs, -1, NULL, 0, NULL, NULL); 
char* mbs = new char[ lengthOfMbs ]; 
WideCharToMultiByte( CP_ACP, 0, wcs, -1, mbs, lengthOfMbs, NULL, NULL);    
delete mbs; 
mbs = NULL; 
MultiByteToWideChar的用法類似 
char* mbs = "中國,你好!I Love You!"; 
int lengthOfWcs = MultiByteToWideChar( CP_ACP, 0, mbs, -1, NULL, 0 ); 
wchar_t* wcs = new wchar_t[ lengthOfWcs ]; 
MultiByteToWideChar( CP_ACP, 0, mbs, -1, wcs, lengthOfWcs ); 
delete wcs; 
wcs = NULL;

下面兩個函數封裝了轉換過程 
#include <Windows.h> 
#include <string>

std::string WcsToMbs( const std::wstring& wcs ) {    
    int lengthOfMbs = WideCharToMultiByte( CP_ACP, 0, wcs.c_str(), -1, NULL, 0, NULL, NULL); 
    char* mbs = new char[ lengthOfMbs ]; 
    WideCharToMultiByte( CP_ACP, 0, wcs.c_str(), -1, mbs, lengthOfMbs, NULL, NULL); 
    std::string result = mbs; 
    delete mbs; 
    mbs = NULL; 
    return result; 
}

std::wstring MbsToWcs( const std::string& mbs ) {    
    int lengthOfWcs = MultiByteToWideChar( CP_ACP, 0, mbs.c_str(), -1, NULL, 0 ); 
    wchar_t* wcs = new wchar_t[ lengthOfWcs ]; 
    MultiByteToWideChar( CP_ACP, 0, mbs.c_str(), -1, wcs, lengthOfWcs ); 
    std::wstring result = wcs; 
    delete wcs; 
    wcs = NULL; 
    return result;

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