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

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的宏替换实现;

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

时间: 2024-12-17 19:10:24

char* wchat_t*互转 及 A2T, T2A宏及其实现原理的相关文章

关于 char 、 wchar_t 、 TCHAR 、 _T() ||| 宏 _T 、 TEXT 、 _TEXT 、 L

char :单字节变量类型,最多表示256个字符, wchar_t :宽字节变量类型,用于表示Unicode字符, 它实际定义在<string.h>里:typedef unsigned short wchar_t. 为了让编译器识别Unicode字符串,必须以在前面加一个"L",定义宽字节类型方法如下: wchar_t c = `A' ;wchar_t * p = L"Hello!" ;wchar_t a[] = L"Hello!"

类型转换(CCstring int string char UTF-8互转)

在做数据转换时,最好包含以下头文件 #include <iostream> #include <cmath> #include <string> #include <sstream> USING_NS_CC; using namespace std; 在cocos2d-x中,也有一个格式刷:CCString(数据转换常常找她做中间人),那么我们要转换类型,可先将起始数据类型刷成CCString然后再转成目的数据类型,这个方法比较方便且实用. //int 转

【转载】cocos2d-x类型转换(CCstring int string char UTF-8互转)以及字符串详解

cocos2d-x中的字符串: <1>:使用 const char* 和 std::string const char* 是C风格的字符串  ,std::string 是C++风格的字符串,它封装了 const char * 初始化 std::string 对象: std::string name = "tony"; std:: string name = std::string("Micheal"); std::string 指针类型: std::st

cocos2d-x类型转换(CCstring int string char UTF-8互转)

http://www.cnblogs.com/leehongee/p/3642308.html //int 转 CCstring int num=5; CCString* ns=CCString::createWithFormat("%d",num); //CCstring 转 int int px = ns->intValue();//将CCString 转换为int的方法,转成float类型有 floatValue() //==========================

java学习-3 string char char[] int 互转

1. string------>int 1.1   “123”----->  1  ,2  ,3 方法1: String s =new String(); s="123"; int i=Integer.parseInt(s.substring(0,2))// int i=123 int i=Integer.parseInt(s.substring(0,1))// int i=12 int i=Integer.parseInt(s.substring(1,2))// int

char wchar 互转 多字符 宽字符 的N种方式

1:  用 CString  如果没有mfc 可以用 ATL 中的 CString  #include <atlstr.h> CStringA v1 = "111"; CStringW v2 = "222"; v2 = v1; 2  : #include <string> std::string ws2s(const std::wstring& ws) { std::string curLocale = setlocale(LC_AL

Linux中_ALIGN宏背后的原理——内存对齐

转载自: http://englishman2008.blog.163.com/blog/static/2801290720114210254690/ 1. 原理    int a;     int size = 8;        <----> 1000(bin) 计算a以size为倍数的下界数:    就让这个数(要计算的这个数)表示成二进制时,最后三位为0就可以达到这个目标.只要下面这个数与a进行"与运算"就可以了:    11111111 11111111 1111

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

</pre><pre> Windows/MFC,C++编程中经常遇到UNICODE.ANSI字符串,并需要对这些字符串进行转换,本文对这些体系与其转换进行了总结. 第一篇:基础篇 3大体系常用函数及解析详见博文:http://blog.csdn.net/u010003835/article/details/47344775  此外,接下来我书写的函数以Windows对char*,TCHAR*,const  TCHAR*的命名规则来书写 不熟悉的童鞋可以参考博文: http://bl

C++11 新特性一增加了 __func__宏

在C11的新特性中,新增加了宏定义 __func__ 用来描述直接得到当函数的名称. 如: const char* hello() {return __func__;} //返回hello. 也可作为初始化参数传递如: struct TestStruct { TestStruct (): name(__func__){}  //返回TestStruct结构体类型. const char* name; }; __VAR_ARGS__ 表示可变参数的宏串,如下 #define LOG(...) {\