標籤:style http color 使用 os io strong ar
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_tcout<<"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 charactertypedef __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 _UNICODEwcout<<"wcout\<\<wstr:"<<wstr<<endl;#elsecout<<"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.aspxwcout<<"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.aspxcout<<"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的宏替換實現;