码迷,mamicode.com
首页 > 编程语言 > 详细

Windows/MFC,C++中的TCHAR体系/char体系/WCHAR体系及其相互转换

时间:2015-08-13 14:33:57      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:c++   windows   unicode   mfc   

</pre><pre>

技术分享

Windows/MFC,C++编程中经常遇到UNICODE、ANSI字符串,并需要对这些字符串进行转换,本文对这些体系与其转换进行了总结。

技术分享

第一篇:基础篇


3大体系常用函数及解析详见博文:http://blog.csdn.net/u010003835/article/details/47344775 


此外,接下来我书写的函数以Windows对char*,TCHAR*,const  TCHAR*的命名规则来书写

不熟悉的童鞋可以参考博文: http://blog.csdn.net/u010003835/article/details/47604553




TCHAR类型,根据环境判断选择字符集 


所需要的字符串转换宏:

 _T()/ TEXT() 根据当前环境对字符串进行转换

其与L的区别见博文: http://blog.csdn.net/u010003835/article/details/47606621



 代表函数及其正确使用方法示例:

LPTSTR temp_t = new TCHAR[MAXN]; 

temp_t[0] = 0; 

_tcscpy(temp_t, _T("Hi, Kemeng~")); 

_tprintf(_T("_tprintf: %s\n"), temp_t); 

delete[] temp_t;


 技术分享






WCHAR类型,使用UNICODE编码


所需要的字符串转换宏:L"XXXX" ,将当前字符串编码为UNICODE格式


测试代码:

LPWSTR temp_w = new WCHAR[MAXN];

temp_w[0] = 0;

wcscpy(temp_w, L"Hi, Kemeng~");

wprintf(L"wprintf: %s\n", temp_w);

delete[] temp_w;


技术分享

常见的错误:

第一种:

wprintf("%s", L"AAAAAA");
技术分享

另一种:

wprintf(L"%s", "AAAAAA");

技术分享


正确写法:

wprintf(L"%s", L"AAAAAA");

技术分享


wcout 貌似更智能一些,会对原始字符串进行转换,估计通过重载,测试后在进行补充


ASCII编码,

不需要字符串转换宏

示例代码

	LPSTR temp_c = new char[MAXN];
	temp_c[0] = 0;
	strcpy(temp_c, "Hi, Kemeng~");
	printf("printf: %s\n", temp_c);
	delete[] temp_c;

技术分享



第二章:进阶篇

WCHAR,TCHAR,char类型字符串之间的转换:

方法一:使用A2T等宏。

宏的名称含义请参见博文:

http://blog.csdn.net/u010003835/article/details/47609975

如果使用这些宏需要包含头文件:

#include <atlbase.h>

并且要声明

USES_CONVERSION;//T2A,A2T,W2T,W2A,A2CW等支持,需要进行此声明


完整的测试代码:

#include <iostream>
#include <windows.h>
#include <atlbase.h>
#include <tchar.h>
using namespace std;


#define MAXN 255

#pragma warning(disable:4996)

int main(){
	USES_CONVERSION;	//T2A,A2T,W2T,W2A,A2CW等支持,需要进行此声明

	//T2A. TCHAR字符串 到 char字符串的转换。
	cout << endl << "TCHAR Convertion to char use T2A" << endl;
	LPTSTR name1_t = _T("Hi, KeMeng~");			
	LPSTR temp1_c = NULL;
	temp1_c = T2A(name1_t);
	
	_tprintf(_T("%s\n"), name1_t);
	printf("%s\n",temp1_c);
	cout << "--------------------------" << endl;

	
	//A2T. char字符串 到 TCHAR字符串的转换
	cout << endl << "char Convertion to TCHAR use A2T" << endl;
	LPSTR name2_c = "Hi, KeMeng~";
	LPTSTR temp2_t = A2T(name2_c);
	printf("%s\n", name2_c);
	_tprintf(_T("%s\n"), temp2_t);
	cout << "--------------------------" << endl;

	
	//W2T. WCHAR字符串 到 TCHAR字符串的转换
	cout << endl << "WCHAR Convertion to TCHAR use W2T" << endl;
	LPWSTR name3_w = L"Hi, KeMeng~";
	LPTSTR temp3_t = T2W(name3_w);
	wprintf(L"%s\n", name3_w);
	_tprintf(_T("%s\n"), temp3_t);
	cout << "--------------------------" << endl;

	//W2T. WCHAR字符串 到 const char字符串的转换
	cout << endl << "WCHAR Convertion to const CHAR use W2CA" << endl;
	LPWSTR name4_w = L"Hi, KeMeng~";
	LPCSTR temp4_a = W2CA(name4_w);
	wprintf(L"%s\n", name4_w);
	printf("%s\n", temp4_a);
	cout << "--------------------------" << endl;


	return 0;
}
技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

Windows/MFC,C++中的TCHAR体系/char体系/WCHAR体系及其相互转换

标签:c++   windows   unicode   mfc   

原文地址:http://blog.csdn.net/u010003835/article/details/47607037

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!