字符类型总结区别wchar_t和char

1.区别wchar_t,char,WCHAR

ANSI:即 char(8bit),可用字符串处理函数:strcat( ),strcpy( ), strlen( )等以str打头的函数。

UNICODE:wchar_t(16bit)是Unicode字符的数据类型,可用字符串处理函数:wcscat(),wcscpy(),wcslen()等以wcs打头的函数。它实际定义在里:

1
2
3
  typedef unsigned short wchar_t;
  另外,在头文件中有这样的定义:typedef wchar_t WCHAR; 所以WCHAR实际就是wchar_t
  为了让编译器识别Unicode字符串,必须以在前面加一个“L”,例如: wchar_t *szTest=L"This is a Unicode string.";

2.TCHAR

  在C语言里面提供了 _UNICODE宏(有下划线),在Windows里面提供了UNICODE宏(无下划线),只要定了_UNICODE宏和UNICODE宏,系统就会自 动切换到UNICODE版本,否则,系统按照ANSI的方式进行编译和运行。只定义了宏并不能实现自动的转换,他还需要一系列的字符定义支持。

1
2
3
4
5
6
7
8
9
1. TCHAR
  如果定义了UNICODE宏则TCHAR被定义为wchar_t。
  typedef wchar_t TCHAR;
 否则TCHAR被定义为char typedef char TCHAR;
2. LPTSTR
 如果定义了UNICODE宏则LPTSTR被定义为LPWSTR。
  typedef LPTSTR LPWSTR;
 否则TCHAR被定义为char typedef LPTSTR LPSTR;
 说明:在使用字符串常量的时候需要使用_TEXT(“MyStr”)或者_T("")来支持系统的自动转换。

3.bstr

字符串BSTR

4.更进一步的字符串以及其指针的类型定义

  由于Win32 API文档的函数列表使用函数的常用名字(例如, “SetWindowText”),所有的字符串都是用TCHAR来定义的。(除了XP中引入的只适用于Unicode的API)。下面列出一些常用的typedefs,你可以在msdn中看到他们。

1
2
3
4
5
6
7
8
9
10

type Meaning in MBCS builds Meaning in Unicode builds
WCHAR wchar_t wchar_t
LPSTR char* char*
LPCSTR const char* const char*
LPWSTR wchar_t* wchar_t*
LPCWSTR wchar_t* wchar_t*
TCHAR TCHAR char wchar_t
LPTSTR TCHAR* TCHAR*
LPCTSTR const TCHAR* const TCHAR*

5.相互转换

wchar_t* 转为 char*

1
2
3
4
5
6
wchar_t* pwszUnicode = L"Holle";  //wcslen(pwsUnicode)=5
int iSize;
char* pszMultiByte;//返回接受字符串所需缓冲区的大小,已经包含字符结尾符'\0'
iSize = WideCharToMultiByte(CP_ACP, 0, pwszUnicode, -1, NULL, 0, NULL, NULL); //iSize =wcslen(pwsUnicode)+1=6
pszMultiByte = (char*)malloc(iSize*sizeof(char)); //不需要 pszMultiByte = (char*)malloc(iSize*sizeof(char)+1);
WideCharToMultiByte(CP_ACP, 0, pwszUnicode, -1, pszMultiByte, iSize, NULL, NULL);

char 转为 wchar_t

1
2
3
4
5
6
char* pszMultiByte = "Holle";  //strlen(pwsUnicode)=5
int iSize; wchar_t* pwszUnicode ;
//返回接受字符串所需缓冲区的大小,已经包含字符结尾符'\0'
iSize = MultiByteToWideChar(CP_ACP, 0, pszMultiByte , -1, NULL, 0); //iSize =wcslen(pwsUnicode)+1=6
pwszUnicode = (wchar_t *)malloc(iSize*sizeof(wchar_t)); //不需要 pwszUnicode = (wchar_t *)malloc((iSize+1)*sizeof(wchar_t))
MultiByteToWideChar(CP_ACP, 0, pszMultiByte , -1, pwszUnicode , iSize);

char*转换成CString

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  若将char*转换成CString,除了直接赋值外,还可使用CString::Format进行。例如:
  char chArray[] = "This is a test";
  char * p = "This is a test";
  或
  LPSTR p = "This is a test";
  或在已定义Unicode应的用程序中
  TCHAR * p = _T("This is a test");
  或
  LPTSTR p = _T("This is a test");
  CString theString = chArray;
  theString.Format(_T("%s"), chArray);
  theString = p;

//方法一
char *p = "test";
CString str(p);

//方法二
char * pFileName = "test";
USES_CONVERSION;
CString s = A2T(pFileName);
//CString s = A2W(pFileName);

CString转换成char*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  若将CString类转换成char*(LPSTR)类型,常常使用下列三种方法:
  方法一,使用强制转换。例如:
  CString theString( "This is a test" );
  LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;

  方法二,使用strcpy。例如:
  CString theString( "This is a test" );
  LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
  _tcscpy(lpsz, theString);
  需要说明的是,strcpy(或可移值Unicode/MBCS的_tcscpy)的第二个参数是 const wchar_t* (Unicode)或const char* (ANSI),系统编译器将会自动对其进行转换。

  方法三,使用CString::GetBuffer。例如:
  CString s(_T("This is a test "));
  LPTSTR p = s.GetBuffer();
  // 在这里添加使用p的代码
  if(p != NULL) *p = _T('\0');
  s.ReleaseBuffer();
  // 使用完后及时释放,以便能使用其它的CString成员函数

//方法四
CString cstr = _T("test")
//声明标识
USES_CONVERSION;
//函数T2A和W2A均支持ATL和MFC中的字符
char * pFileName = T2A(cstr);
//char * pFileName = W2A(cstr); //也可实现转换

//注意:有时候可能还需要添加引用#include <afxpriv.h>

BSTR转换成char*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  方法一,使用ConvertBSTRToString。例如:
  #include
  #pragma comment(lib, "comsupp.lib")
  int _tmain(int argc, _TCHAR* argv[]){
    BSTR bstrText = ::SysAllocString(L"Test");
    char* lpszText2 = _com_util::ConvertBSTRToString(bstrText);
    SysFreeString(bstrText); // 用完释放
    delete[] lpszText2;
    return 0;
  }
  方法二,使用_bstr_t的赋值运算符重载。例如:
  _bstr_t b = bstrText;
  char* lpszText2 = b;//底层还是调的_com_util::ConvertBSTRToString(bstrText);
```  
### char*转换成BSTR

  方法一,使用SysAllocString等API函数。例如:
  BSTR bstrText = ::SysAllocString(L”Test”);
  BSTR bstrText = ::SysAllocStringLen(L”Test”,4);
  BSTR bstrText = ::SysAllocStringByteLen(“Test”,4);

  方法二,使用COleVariant或_variant_t。例如:
  //COleVariant strVar(“This is a test”);
  _variant_t strVar(“This is a test”);
  BSTR bstrText = strVar.bstrVal;

  方法三,使用_bstr_t,这是一种最简单的方法。例如:
  BSTR bstrText = _bstr_t(“This is a test”);//底层还是调的方法五

  方法四,使用CComBSTR。例如:
  BSTR bstrText = CComBSTR(“This is a test”);
  或
  CComBSTR bstr(“This is a test”);
  BSTR bstrText = bstr.m_str;

  方法五,使用ConvertStringToBSTR。例如:
  char* lpszText = “Test”;
  BSTR bstrText = _com_util::ConvertStringToBSTR(lpszText);
//使用前需要加上头文件comutil.h
反之可以使用char *p=_com_util::ConvertBSTRToString(b);   

1
### CString转换成BSTR

  通常是通过使用CStringT::AllocSysString来实现。例如:
  CString str(“This is a test”);
  BSTR bstrText = str.AllocSysString();
  …
  SysFreeString(bstrText); // 用完释放

1
2
3
  注意:用完之后必须使用SysFreeString 释放!!!

### BSTR转换成CString

方法一:
  BSTR bstrText = ::SysAllocString(L”Test”);
  CStringA str;
  str.Empty();
  str = (LPCSTR)bstrText;
方法二:
  BSTR bstrText = ::SysAllocString(L”Test”);
  CStringA str(bstrText);

1
2
3
4
5
### CString转_bstr_t
```  
_bstr_t bstr;
CString strSql;
bstr = (_bstr_t)strSql;

_bstr_t转CString

1
2
3
_bstr_t bstr;
CString strSql;
strSql = (LPCSTR)bstr;

BSTR 转LPCTSTR

1
2
3
4
5
6
7
//方法一:
_bstr_t strstr = bstrtext;
LPCTSTR lpctstrname = strstr;

//方法二:
CString str = bstrtext;
LPCTSTR lpctstrname = (LPCTSTR)str;

LPCTSTR转CString

1
2
3
4
5
6
7
8
9
10

//方法一:
LPCTSTR lpctStr;
CString cStr=lpctStr;

//方法二:
LPCTSTR p;
CString str="hello";
p=str.GetBuffer(str.GetLength());
str.ReleaseBuffer();

CString与LPCWSTR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
两者的不同:LPCWSTR 是Unicode字符串指针,初始化时串有多大,申请空间就有多大,以后存贮若超过则出现无法预料的结果,这是它与CString的不同之处。而CString是一个串类,内存空间类会自动管理。

CString转换成LPCWSTR

方法一:CString strFileName;

LPCWSTR lpcwStr = strFileName.AllocSysString();

方法二:CString str=_T("TestStr");
USES_CONVERSION;
LPCWSTR lpcwStr = A2CW((LPCSTR)str);

MFC中CString和LPSTR是可以通用,其中A2CW表示(LPCSTR) -> (LPCWSTR),USER_CONVERSION表示用来定义一些中间变量,在使用ATL的转换宏之前必须定义该语句。

LPCWSTR转换成CString

LPCWSTR lpcwStr = L"TestWStr";
CString str(lpcwStr);

CString转换成LPSTR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
方法一:CString strFileName;

LPSTR lpStr = strFileName.GetBuffer();

strFileName.ReleaseBuffer();

方法二:CString strFileName;

LPSTR lpStr = (LPSTR)(LPCSTR)strFimeName;

LPSTR转换成CString:

LPSTR lpStr = L"TestStr";
CString str(lpStr);

注意:CString和LPCSTR可直接转换,如下:

CString str;

LPCSTR lpcStr = (LPCSTR)str;

ANSI、Unicode和宽字符之间的转换

  • 方法一,使用MultiByteToWideChar将ANSI字符转换成Unicode字符,使用WideCharToMultiByte将Unicode字符转换成ANSI字符。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    //将单字节char*转化为宽字节wchar_t*  
    wchar_t* AnsiToUnicode( const char* szStr )
    {
    int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0 );
    if (nLen == 0)
    {
    return NULL;
    }
    wchar_t* pResult = new wchar_t[nLen];
    MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen );
    return pResult;
    }

    //将宽字节wchar_t*转化为单字节char*
    inline char* UnicodeToAnsi( const wchar_t* szStr )
    {
    int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );
    if (nLen == 0)
    {
    return NULL;
    }
    char* pResult = new char[nLen];
    WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL );
    return pResult;
    }
  • 方法二,使用“_T”将ANSI转换成“一般”类型字符串,使用“L”将ANSI转换成Unicode,而在托管C++环境中还可使用S将ANSI字符串转换成String对象。例如:
      TCHAR tstr[] = _T(“this is a test”);
      wchar_t wszStr[] = L”This is a test”;
      String
    str = S”This is a test”;

  方法三,使用ATL 7.0的转换宏和类。ATL7.0在原有3.0基础上完善和增加了许多字符串转换宏以及提供相应的类,它具有如图3所示的统一形式:
  其中,第一个C表示“类”,以便于ATL 3.0宏相区别,第二个C表示常量,2表示“to”,EX表示要开辟一定大小的缓冲。SourceType和DestinationType可以是A、   T、W和OLE,其含义分别是ANSI、Unicode、“一般”类型和OLE字符串。例如,CA2CT就是将ANSI转换成一般类型的字符串常量。下面 是一些示例代码:
  LPTSTR tstr= CA2TEX<16>(“this is a test”);
  LPCTSTR tcstr= CA2CT(“this is a test”);
  wchar_t wszStr[] = L”This is a test”;
  char* chstr = CW2A(wszStr);

https://www.cnblogs.com/zhoug2020/archive/2012/06/13/2547463.html

http://www.imkevinyang.com/2010/06/%E5%85%B3%E4%BA%8E%E5%AD%97%E7%AC%A6%E7%BC%96%E7%A0%81%EF%BC%8C%E4%BD%A0%E6%89%80%E9%9C%80%E8%A6%81%E7%9F%A5%E9%81%93%E7%9A%84.html

http://cppblog.com/lizao2/articles/169250.html