CString string 的相互转化,宽字符,多字符的转换
来源:互联网 发布:天津市软件学院答辩 编辑:程序博客网 时间:2024/04/27 22:15
在本文开始之处,先简要地说一下何为短字符和宽字符.
所谓的短字符,就是用8bit来表示的字符,典型的应用是ASCII码.(也就是我们常说的多字符集(普通的字符串就是这个))而宽字符
,顾名思义,就是用16bit表示的字符,典型的有UNICODE(MFC中的CString就是一个).关于windows下的ASCII和UNICODE的更多信息,
可以参考这两本经典著作:《windows 程序设计》,《windows 核心编程》.这两本书关于这两种字符都有比较详细的解说.
L表示long, P表示指针,C表示constant,W表示wide,T:win32环境中有一个_T宏,用来标识字符是否采用Unicode编码(两字节表示一个字符),若程序中定义了Unicode,该字符/字符串被作为Unicode字符串,否则就是标准的ANSI(单字节表示一个字符)字符串。
STR:表示这个变量是一个字符串。
1:LPTSTR = char*:
2:LPCSTR(多个C) 表示常字符串
3:LPTSTR (多个T) 表示取决于Unicode宏是否定义
4:LPCTSTR (多个C) 解释同2
5:LPWSTR 表示Unicode字符串(宽字符串)
6:LPCWSTR (多个C) 解释同2
一个例子,CString string 的相互转化
////string 到 CString (短字符到宽字符) CString strTemp1;string strTemp_1 = "sdddasdfasdf";int nLen = MultiByteToWideChar(CP_ACP, 0, strTemp_1.c_str(), -1, NULL, 0);MultiByteToWideChar(CP_ACP, 0, strTemp_1.c_str(), -1, strTemp1.GetBuffer(), nLen);////CString 到 string (宽字符到多//字///符)CString strTemp1("sdddasdfasdf");string strTemp_1 ;DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, strTemp1.GetBuffer(), -1, NULL, 0, NULL, FALSE);WideCharToMultiByte(CP_OEMCP, NULL, strTemp1.GetBuffer(), -1,(LPSTR)strTemp_1.c_str(), dwNum, NULL, FALSE);
1)多字符(短字符跟多字符一个意思)转换宽多字符
char sText[20] = {"多字节字符串!OK!"};/////多字符集(8bit)
我们需要知道转化后的宽字符需要多少个数组空间,
所幸,我们能够确知所需要的数组空间.
我们只需要将MultiByteToWideChar()的第四个形参设为-1,即可返回所需的短字符数组空间的个数:
DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, sText, -1, NULL, 0);
接下来,我们只需要分配响应的数组空间:
wchar_t *pwText;
pwText = new wchar_t[dwNum];
if(!pwText)
{
delete []pwText;
}
接着,我们就可以着手进行转换了.在这里以转换成ASCII码做为例子:
MultiByteToWideChar (CP_ACP, 0, pwText, -1, sText, dwSize);
最后,使用完毕当然要记得释放占用的内存:
delete []pwText;
(2)宽字符集转换为多字符集(短字符跟多字符一个意思)
wchar_t wText[20] = {L"宽字符转换实例!OK!"};
DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL, wText,-1,NULL,0,NULL,FALSE);
char *psText;
psText = new char[dwNum];
if(!psText)
{
delete []psText;
}
WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);
delete []psText;
- CString string 的相互转化,宽字符,多字符的转换
- char*, string, CString, String^的相互转换
- char*, string, CString, String^的相互转换
- char*, string, CString, String^的相互转换
- unicode字符和多字节字符的相互转换接口
- CString,string,char*之间的相互转换
- CString string char*之间的相互转换
- CString char string等的相互转换
- CString,TCHAR*,string之间的相互转换
- char[] CString string 的相互转换
- Cstring --string -- char*之间的相互转换
- CString和string的相互转换
- CString与string的相互转换
- char[] CString string 的相互转换
- char,string,cstring之间的相互转换
- CString, char*, string的相互转换
- char[] CString string 的相互转换
- CString与char * ,string 的相互转换
- VMware下用vdiskmanager命令+gparted分区工具扩大Linux虚拟机硬盘容量
- Struts2.0中ActionInvocation使用
- linux高级编程的理解
- 关于ARM的22个常用概念
- SQLServer As
- CString string 的相互转化,宽字符,多字符的转换
- Codeforece#219(DIV2) C题
- Cookie的基础知识
- 20131214-第七天
- 在异常处理中处理析构函数
- ++i和i++效率谁高
- android scrollview 嵌套listview 或者listview嵌套listview 计算子view高度
- Fedora18设置终端快捷键 和 桌面快捷方式
- UltraGrid中绑定UltraTextEditor在AfterEditorButtonCloseUp事件中获得所属UltraGridCell