Relationship: char, wchar_t, TCHAR, _T(), wstring, string

初学C/C++,char, wchar_t, TCHAR, ACHAR, _T()这几个类型的差异和联系曾经让我很是头疼,在此做一下简单的归纳总结,希望能给各位刚入门的菜菜们一点帮助。

char :

单 字节变量类型,最多表示256个字符,在ANSI C中包括:26 lowercase letters, 26 uppercase letters, 10 digits, 32 symbols, 33 control codes, and a space, for a total of 128 codes.

wchar_t :

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

定义宽字节类型方法如下:

wchar_t c  =  `A '  ; 
wchar_t  *  p  =  L " Hello! "  ; 
wchar_t a[] 
=  L " Hello! "  ; 

其中,宽字节类型每个变量占用2个字节,故上述数组a的sizeof(a) = 14。

TCHAR / _T( ) :

TCHAR.H provides a set of alternative names for the normal run-time library functions requiring string parameters (for example, _tprintf and _tcslen). These are sometimes referred to as "generic"function names because they can refer to either the Unicode or non-Unicode versions of the functions. TCHAR.H also solves the problem of the two character data types with a new data type named TCHAR.

如果在程序中既包括ANSI又包括Unicode编 码,需要包括头文件tchar.h。TCHAR是定义在该头文件中的宏,它视你是否定义了_UNICODE宏而定义成:
定义了 _UNICODE:    typedef wchar_t TCHAR ;
没有定义_UNICODE: typedef char TCHAR ;

_T( )也是定义在该头文件中的宏,视是否定义了_UNICODE宏而定义成:
定义了 _UNICODE:    #define _T(x)  L##x
没有定义_UNICODE: #define _T(x)  x

注 意:如果在程序中使用了TCHAR,那么就不应该使用ANSI的strXXX函数或者Unicode的wcsXXX函数了,而必须使用tchar.h中定 义的_tcsXXX函数。

以strcpy函数为例子,总结一下:

// 如果你想使用ANSI字符串,那么请使用这一套写法:
char  szString[ 100 ];
strcpy(szString,
" test " );
// 如果你想使用Unicode字符串,那么请使用这一套:
wchar_t szString[ 100 ];
wcscpy(szString,L
" test " );
// 如果你想通过定义_UNICODE宏,而编译ANSI或者Unicode字符串代码:
TCHAR szString[ 100 ];
_tcscpy(szString,_TEXT(
" test " ));


string / wstring :

string和wstring均定义在string头文件中,其中string类型变量中 每个单元为char型字符,wstring为wchar_t型字符。
定义方法如下:
string str("abcd");
wstring wstr(L"中国人");

各转换方法:

// char* to string : 
char   * =   " abcde " ;
string  str  =  a;

// string to char*
char   * =  str.c_str();

// wchar_t* to wstring
wchar_t  * =  L " abcde " ;
wstring wstr 
=  b;

// wstring to wchar_t*
wchar_t  * =  wstr.c_str();

// wstring to string 
std:: string  ws2s( const  std::wstring &  ws)
{
    std::
string  curLocale  =  setlocale(LC_ALL, NULL);      
        setlocale(LC_ALL, 
" chs " );
        
const  wchar_t *  _Source  =  ws.c_str();
        size_t _Dsize 
=   2   *  ws.size()  +   1 ;
        
char   * _Dest  =   new   char [_Dsize];
        memset(_Dest,
0 ,_Dsize);
        wcstombs(_Dest,_Source,_Dsize);
        std::
string  result  =  _Dest;
        delete []_Dest;
        setlocale(LC_ALL, curLocale.c_str());

        
return  result;
}


// string to wstring
std::wstring s2ws( const  std:: string &  s)
{
      setlocale(LC_ALL, 
" chs " ); 
      
const   char *  _Source  =  s.c_str();
      size_t _Dsize 
=  s.size()  +   1 ;
      wchar_t 
* _Dest  =   new  wchar_t[_Dsize];
      wmemset(_Dest, 
0 , _Dsize);
      mbstowcs(_Dest,_Source,_Dsize);
      std::wstring result 
=  _Dest;
      delete []_Dest;
      setlocale(LC_ALL, 
" C " );

      
return  result;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值