char* wchat_t*互轉 及 A2T, T2A宏及其實現原理

來源:互聯網
上載者:User

標籤: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的宏替換實現;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.