C++ TCHAR* 与char* 互转
C++ TCHAR* 与char* 互转
在MSDN中有这么一段:
Note: The ANSI code pages can be different on different computers, or can be changed for a single computer, leading to data corruption. For the most consistent results, applications should use Unicode, such as UTF-8 (code page 65001) or UTF-16, instead of a specific code page, unless legacy standards or data formats prevent the use of Unicode. If use of Unicode is not possible, applications should tag the data stream with the appropriate encoding name when protocols allow it. HTML, XML, and HTTP files allow tagging, but text files do not.
大概意思就是说ANSI页码的文件可能会根据不同的计算机而改变,建议使用unicode(UTF-8或UTF-16)的编码
而VS默认就使用unicode编码
ANSI:char, string …
UNICODE: TCHAR, wchar_t …
MSDN里给我们提花了两个函数
MultiByteToWideChar:
int MultiByteToWideChar( UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar );
WideCharToMultiByte:
int WideCharToMultiByte( UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar );
互转的实现:
char* StrUtils::TCHAR2char( const TCHAR* STR )
{
//返回字符串的长度
int size = WideCharToMultiByte(CP_ACP, 0, STR, -1, NULL, 0, NULL, FALSE);
//申请一个多字节的字符串变量
char* str = new char[sizeof(char) * size];
//将STR转成str
WideCharToMultiByte(CP_ACP, 0, STR, -1, str, size, NULL, FALSE);
return str;
}
TCHAR* StrUtils::char2TCAHR( const char* str )
{
int size = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
TCHAR* retStr = new TCHAR[size * sizeof(TCHAR)];
MultiByteToWideChar(CP_ACP, 0, str, -1, retStr, size);
return retStr;
}
C++ TCHAR* 与char* 互转的更多相关文章
- TCHAR和CHAR类型的互转,string 转lpcwstr
https://www.cnblogs.com/yuguangyuan/p/5955959.html 没有定义UNICODE,所以它里面的字符串就是简单用" "就行了,创建工程的时 ...
- TCHAR和CHAR类型的互转
http://blog.csdn.net/ahjxly/article/details/8494217 http://blog.csdn.net/b_h_l/article/details/75815 ...
- c++ string 与 char 互转 以及base64
c++ string 与 char 互转 很简单如下 ] = {'A','B','C','D','E'}; printf("%s\n",bts); //char to string ...
- CString与char *互转总结
1 前言 今天在网上看论坛,发现大家对CString与Char *互转各说一词,其实我发现提问者所说的情况与回答问题的人完全不是同一情况,这里做一总结. 首先大家得清楚一件事,一般在网上提出问题的人大 ...
- MFC TCHAR 和CHAR相互转换
没有定义UNICODE,所以它里面的字符串就是简单用" "就行了,创建工程的时候包含了UNICODE定义,就必须对TCHAR和char进行转换. 首先是把TCHAR转为char / ...
- wchat_t与char互转
C++ Code 1234567891011121314151617181920212223242526 //窄字符转宽字符 void ConvertA2W(wchar_t* the_strw ...
- C#string与char互转
string s = "asdf"; //字符转char char[] c = s.ToCharArray(); Console.WriteLine(s[]); //char转st ...
- QString和char*互转
1. QString转为char * // QString转QByteArray QByteArray sr = strQ.toLocal8Bit(); int len = sr.length(); ...
- CString和char互转,十六进制的BYTE转CString
CString转char: CString m_Readcard; char ReaderName[22]; strcpy((char*)&ReaderName,(LPCTSTR)m_Read ...
随机推荐
- node(s) didn‘t match node selector.
k8s集群中,有pod出现了 Pending ,通过 kubectl describe pod 命令,发现了如下报错 0/4 nodes are available: 1 node(s) had ta ...
- 继承及super关键字
继承 继承的本质是对某一批类的抽象,从而实现对世界更好的建模 extend的意思是"扩展",子类是父类的扩展. Java中类只有单继承,没有多继承:儿子只能有一个亲生爸爸,一个爸爸 ...
- .Net Core AOP之AuthorizeAttribute
一.简介 在.net core 中Filter分为以下六大类: 1.AuthorizeAttribute(权限验证) 2.IResourceFilter(资源缓存) 3.IActionFilter(执 ...
- [题解]Codeforces Round #519 - B. Lost Array
[题目] B. Lost Array [描述] Bajtek有一个数组x[0],x[1],...,x[k-1]但被搞丢了,但他知道另一个n+1长的数组a,有a[0]=0,对i=1,2,...,n.由此 ...
- [题解]第十一届北航程序设计竞赛预赛——A.模式
题目描述 输入一个学号,判断是计算机系or软件学院or其他院系. 解题思路 水题,直接判断or除以10000都可以.不废话,直接上代码. 1 #include <iostream> 2 # ...
- [旧][Android] LayoutInflater 工作流程
备注 原发表于2016.06.20,资料已过时,仅作备份,谨慎参考 前言 感觉很长时间没写文章了,这个星期因为回家和处理项目问题,还是花了很多时间的.虽然知道很多东西如果只是看一下用一次,很快就会遗忘 ...
- 5大知名的BI工具对比介绍
工欲善其事,必先利其器.企业对于BI工具的需求,已经刻不容缓.国内国外的BI工具不少,如Tableau.FineBI.Power BI.Smartbi等等.本文就对当下市面上最热门的5款知名的BI工具 ...
- Java高级部分概要笔记
复习 多线程 程序.进程.线程的概念.区别 并行.并发 创建线程的四种方式:继承Thread.实现Runnable接口.实现Callable接口.线程池 线程的生命周期 线程的同步方式,三种:同步方法 ...
- Bagging与随机森林
Bagging Bagging是并行式集成学习算法最著名的代表,基于自助采样法(bootstrap sampling). 给定m个样本的数据集,选取m次,每次选1个样本,构成一个新的样本集,其中有的样 ...
- ssh执行命令或者脚本里最好先加上 source /etc/profile
记: 写一次shell脚本,发现总提示 找不到JAVA_HOME .明明已经配了环境变量,并且java -version也已经能输出信息,但就是提示. 后来在 脚本中加入第一句代码 source /e ...