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;

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