方法一:使用API:WideCharToMultiByte进行转换
01.
#include <afx.h>
02.
03.
int
_tmain(
int
argc, _TCHAR* argv[])
04.
{
05.
CString cstr = _T(
"test测试"
);
06.
07.
//获取宽字节字符的大小,大小是按字节计算的
08.
int
len = WideCharToMultiByte(CP_ACP,
0
,cstr,cstr.GetLength(),NULL,
0
,NULL,NULL);
09.
10.
11.
//为多字节字符数组申请空间,数组大小为按字节计算的宽字节字节大小
12.
char
* pbuffer =
new
char
[len+
1
];
//以字节为单位
13.
14.
15.
//宽字节编码转换成多字节编码
16.
WideCharToMultiByte(CP_ACP,
0
,cstr,cstr.GetLength(),pbuffer,len,NULL,NULL);
17.
18.
19.
pbuffer[len] =
'\0'
;
20.
21.
22.
if
(pbuffer)
23.
{
24.
delete [] pbuffer;
25.
pbuffer = NULL;
26.
}
27.
28.
29.
return
0
;
30.
}
方法二:使用函数:T2A、W2A
01.
#include <afxpriv.h>
02.
03.
int
_tmain(
int
argc, _TCHAR* argv[])
04.
{
05.
CString cstr = _T(
"test测试"
);
06.
07.
08.
//声明标识符
09.
USES_CONVERSION;
10.
11.
12.
char
*pbuffer = T2A(cstr);
13.
char
*pbuf = W2A(cstr);
14.
15.
16.
return
0
;
17.
}
2、Unicode下char *转换为CString
方法一:使用API:MultiByteToWideChar进行转换
01.
#include <afx.h>
02.
03.
04.
int
_tmain(
int
argc, _TCHAR* argv[])
05.
{
06.
char
* pchar =
"test测试"
;
07.
08.
int
charLen = strlen(pchar);
//计算char *数组大小,以字节为单位,一个汉字占两个字节
09.
10.
int
len = MultiByteToWideChar(CP_ACP,
0
,pchar,charLen,NULL,
0
);
//计算多字节字符的大小,按字符计算。
11.
12.
TCHAR *buf =
new
TCHAR[len +
1
];
//为宽字节字符数组申请空间,数组大小为按字节计算的多字节字符大小
13.
14.
15.
MultiByteToWideChar(CP_ACP,
0
,pchar,charLen,buf,len);
//多字节编码转换成宽字节编码
16.
17.
18.
buf[len] =
'\0'
;
19.
20.
21.
//将TCHAR数组转换为CString
22.
CString pWideChar;
23.
pWideChar.Append(buf);
24.
25.
26.
if
(buf)
27.
{
28.
delete [] buf;
29.
buf = NULL;
30.
}
31.
32.
33.
return
0
;
34.
}
方法二:使用函数:A2T、A2W
01.
#include <afx.h>
02.
03.
int
_tmain(
int
argc, _TCHAR* argv[])
04.
{
05.
char
* pchar =
"test测试"
;
06.
07.
USES_CONVERSION;
08.
09.
CString cstr = A2T(pchar);
10.
CString cstr1 = A2W(pchar);
11.
12.
return
0
;
13.
}
方法三:使用_T宏,将字符串转换为宽字符
书写代码使用TEXT("")或_T(""),文本在UNICODE和非UNICODE程序里都通用
01.
#include <afx.h>
02.
03.
int
_tmain(
int
argc, _TCHAR* argv[])
04.
{
05.
CString cstr = _T(
"test测试"
);
06.
CString cstr1 =
"test测试"
;
07.
08.
return
0
;
09.
}
3、将char*转化为wchar_t*
01.
#include <afx.h>
02.
wchar_t* AnsiToUnicode(
const
char
* szStr)
03.
{
04.
int
nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -
1
, NULL,
0
);
05.
if
(nLen ==
0
)
06.
return
NULL;
07.
08.
wchar_t* pResult =
new
wchar_t[nLen];
09.
MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -
1
, pResult,
nLen );
10.
return
pResult;
11.
}
4、将wchar_t*转化为char*
01.
#include <afx.h>
02.
char
* UnicodeToAnsi(
const
wchar_t* szStr)
03.
{
04.
int
nLen = WideCharToMultiByte( CP_ACP,
0
, szStr, -
1
, NULL,
0
, NULL, NULL );
05.
if
(nLen ==
0
)
06.
return
NULL;
07.
08.
09.
char
* pResult =
new
char
[nLen];
10.
WideCharToMultiByte( CP_ACP,
0
, szStr, -
1
, pResult, nLen, NULL, NULL );
11.
return
pResult;
12.
}
5、BSTR转换成char*
1.
方法一,使用ConvertBSTRToString。例如:
2.
char
* lpszText2 = _com_util::ConvertBSTRToString(bstrText);
3.
SysFreeString(bstrText);
// 用完释放
4.
delete[] lpszText2;
5.
6.
方法二,使用_bstr_t的赋值运算符重载。例如:
7.
_bstr_t b = bstrText;
8.
char
* lpszText2 = b;
6、char*转换成BSTR
01.
方法一,使用SysAllocString等API函数。例如:
02.
BSTR bstrText = ::SysAllocString(L
"test测试"
);
03.
04.
方法二,使用COleVariant或_variant_t。例如:
05.
_variant_t strVar(
"test测试"
);
06.
BSTR bstrText = strVar.bstrVal;
07.
08.
方法三,使用_bstr_t。例如:
09.
BSTR bstrText = _bstr_t(
"test测试"
);
10.
11.
方法四,使用CComBSTR。例如:
12.
BSTR bstrText = CComBSTR(
"test测试"
);
13.
或
14.
CComBSTR bstr(
"test测试"
);
15.
BSTR bstrText = bstr.m_str;
16.
17.
方法五,使用ConvertStringToBSTR。例如:
18.
char
* lpszText =
"test测试"
;
19.
BSTR bstrText = _com_util::ConvertStringToBSTR(lpszText);
7、CString转换成BSTR
1.
通常是通过使用CStringT::AllocSysString来实现。例如:
2.
CString str(
"test测试"
);
3.
BSTR bstrText = str.AllocSysString();
4.
SysFreeString(bstrText);
// 用完释放
8、BSTR转换成CString
1.
BSTR bstrText = ::SysAllocString(L
"test测试"
);
2.
CStringA str;
3.
str.Empty();
4.
str = bstrText;
5.
或
6.
CStringA str(bstrText);