VC char和TCHAR之间转换
来源:互联网 发布:电力猫网络dns不正常 编辑:程序博客网 时间:2024/06/05 05:58
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程序
- 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);
4、启动结果如下
- VC char和TCHAR之间转换
- VC char和TCHAR之间转换
- VC char和TCHAR之间转换
- VC char和TCHAR之间转换
- VC char和TCHAR之间转换
- char和TCHAR之间转换
- Tchar 和 char 之间的转换
- TCHAR和char转换
- TCHAR和char转换
- 关于vs2005 TCHAR和char之间的转换
- CString、TCHAR*、char*之间的转换函数
- CString string char TCHAR 之间的转换
- VC TCHAR转char ~ BSTR转换成char*
- MFC TCHAR 和CHAR相互转换
- MFC TCHAR 和CHAR相互转换
- TCHAR和CHAR间的互相转换
- string,wstring,CString,TCHAR,char*之间常用转换
- string,wstring,CString,TCHAR,char*之间常用转换
- Maven install报MojoExecutionException
- linux安装bcompare
- 关于调用listFile()在遍历含有乱码文件时出现input is not valid Modified UTF-8错误的解决方法
- ES6之------------------let,const
- z-index的用法,不生效问题
- VC char和TCHAR之间转换
- Java并发编程:volatile关键字解析
- 2048
- clear清除
- ARM Linux 3.x的设备树(Device Tree)
- [k8s]k8s架构图解
- webview加载富文本混淆后出错
- 前端一种页脚的写法,css控制footer
- 线程的状态及转化