char*, wchat_t*互转 及 A2T, T2A宏及其实现原理

本文的内容是在 上一篇博文 ASCI, GB2312, UNICODE, UTF8编码比较 的基础上的。

char :单字节变量类型,表示ASCII码。

wchar_t :宽字节变量类型,用于表示Unicode字符。在<string.h>定义为:typedef unsigned short wchar_t。

TCHAR: VS下的中间类型。在“使用Unicode字符集”下TCHAR定义为wchar_t,在字符集 “未设置” 条件下TCHAR定义为char。


A2T,及T2A是两个非常有用的宏,可以用于实现char*和wchar_t*字符串之间的转换。宏的实现也将会在测试代码中给出,不感兴趣的直接跳过。费话不多说,直接上测试代码,参考代码注释。


// demo1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>
#include <cstring>
using namespace std;
#include <atlbase.h>
#include <Windows.h>

void test_L_macro()
{ 
	char astr[] = "hello";
	cout<<"sizeof(astr):"<<sizeof(astr)<<endl
		<<"strlen(astr):"<<strlen(astr)<<endl;


	//L"str"表示将ANSI字符串转换成unicode的字符串,就是每个字符占用两个字节。
	wchar_t wstr[] = L"hello"; //typedef unsigned short wchar_t
	cout<<"sizeof(wstr)"<<sizeof(wstr)<<endl
		<<"wcslen(wstr):"<<wcslen(wstr)<<endl;
}


void test_T_macro()
{
	/*
	#ifdef   _UNICODE
		#define __T(x)      L ## x
	#else
		#define __T(x)      x
	#endif


	#define _T(x)       __T(x)
	#define _TEXT(x)    __T(x)
	#define TEXT(quote) __TEXT(quote)


	#ifdef _UNICODE   
		typedef char TCHAR;   
	#else   
		typede wchar_t TCHAR;   
	#endif 
	*/
	//如果在程序中使用了TCHAR,那么就不应该使用ANSI的strXXX函数或者Unicode的wcsXXX函数了,
	//而必须使用tchar.h中定义的_tcsXXX函数。
	TCHAR buf[] = _T("hello");
	cout<<"sizeof(buf):"<<sizeof(buf)<<endl
		<<"_tcslen(lpBuf):"<<_tcslen(buf)<<endl; 

	/*
	#ifdef _UNICODE   
		typedef wchar_t WCHAR;// wc,   16-bit UNICODE character
		typedef __nullterminated WCHAR *LPWSTR;
		typedef LPWSTR PTSTR, LPTSTR;
	#else   
		typedef char CHAR;
		typedef __nullterminated CHAR *LPSTR;
		typedef LPSTR LPTSTR;
	#endif 
	*/

	LPTSTR lpBuf = TEXT("hello");
	cout<<"_tcslen(lpBuf):"<<_tcslen(lpBuf)<<endl;  
}

void test_A2T()
{
	USES_CONVERSION;
	char *astr = "hello";
	LPTSTR wstr = A2T(astr);

#ifdef _UNICODE
	wcout<<"wcout\<\<wstr:"<<wstr<<endl;
#else
	cout<<"cout\<\<wstr:"<<wstr<<endl;
#endif
}

void test_A2T_()
{
	int _convert = 0;
	(_convert); 
	UINT _acp = ATL::_AtlGetConversionACP() ; 
	(_acp); 
	LPCWSTR _lpw = 0; 
	(_lpw); 
	LPCSTR _lpa = 0; 
	(_lpa);

	char *astr = "hello";

	LPTSTR wstr = ( ((_lpa = astr) == 0) ? 0 : ( _convert = (lstrlenA(_lpa)+1), (2147483647/2<_convert)? 0 : AtlA2WHelper((LPWSTR) _alloca(_convert*sizeof(WCHAR)), _lpa, _convert, _acp)));
	//p.s. AtlA2WHelper调用了函数MultiByteToWideChar
	//_alloca函数用于在栈上分配内存,参考:http://msdn.microsoft.com/en-us/library/wb1s57t5.aspx

	wcout<<"wcout\<\<wstr:"<<wstr<<endl;

}

void test_T2A()
{
	USES_CONVERSION;	
	LPTSTR wstr = _T("hello");
	char *astr = T2A(wstr);

	cout<<"cout\<\<astr:"<<astr<<endl;
}

void test_T2A_()
{
	int _convert = 0; 
	(_convert); 
	UINT _acp = ATL::_AtlGetConversionACP() ; 
	(_acp); 
	LPCWSTR _lpw = 0; 
	(_lpw); 
	LPCSTR _lpa = 0; 
	(_lpa);	

	LPTSTR wstr = L"hello";

	char *astr = ( ((_lpw = wstr) == 0) ? 0 : ( (_convert = (lstrlenW(_lpw)+1), (_convert>2147483647/2) ? 0 : AtlW2AHelper((LPSTR) _alloca(_convert*sizeof(WCHAR)), _lpw, _convert*sizeof(WCHAR), _acp))));
	//p.s. AtlW2AHelper调用了函数WideCharToMultiByte
	//_alloca函数用于在栈上分配内存,参考:http://msdn.microsoft.com/en-us/library/wb1s57t5.aspx

	cout<<"cout\<\<astr:"<<astr<<endl;
}

int main()
{
	test_L_macro();
	test_T_macro();

	test_A2T();
	test_A2T_();

	test_T2A();
	test_T2A_();
}

P.S.

test_A2T_ 是 test_A2T的宏替换实现;test_T2A_ 是 test_T2A的宏替换实现;





  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值