VC++中char和TCHAR之间转换

char:计算机编程语言(c、c++、java等)中可容纳单个字符的一种基本数据类型。java

TCHAR:为了知足Unicode编码,对char的扩展,即_T(“str”)表示TCHAR类型c++

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 的使用spa

 ===========================code

 

 应用实例:经过system函数程序调用启动msc程序orm

 

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程序  
}  
相关文章
相关标签/搜索