Unicode编码下CString、char*BSTR相互转换,char*wchar_t*相互转换

Unicode编码下CString、char*、BSTR相互转换,char*、wchar_t*相互转换
1、Unicode编码下CString转为char*

方法一:使用API:WideCharToMultiByte进行转换
#include <afx.h>

int _tmain(int argc, _TCHAR* argv[])
{
	CString cstr = _T("test测试");
	
	//获取宽字节字符的大小,大小是按字节计算的
	int len = WideCharToMultiByte(CP_ACP,0,cstr,cstr.GetLength(),NULL,0,NULL,NULL);


	//为多字节字符数组申请空间,数组大小为按字节计算的宽字节字节大小
	char * pbuffer = new char[len+1];   //以字节为单位


	//宽字节编码转换成多字节编码
	WideCharToMultiByte(CP_ACP,0,cstr,cstr.GetLength(),pbuffer,len,NULL,NULL);


	pbuffer[len] = '\0'; 


	if (pbuffer)
	{
		delete [] pbuffer;
		pbuffer = NULL;
	}


	return 0;
}


方法二:使用函数:T2A、W2A

#include   <afxpriv.h>

int _tmain(int argc, _TCHAR* argv[])
{
	CString cstr = _T("test测试");


	//声明标识符
	USES_CONVERSION;


	char *pbuffer = T2A(cstr);   
	char *pbuf = W2A(cstr);


	return 0;
}
2、Unicode下char *转换为CString

方法一:使用API:MultiByteToWideChar进行转换
#include <afx.h>


int _tmain(int argc, _TCHAR* argv[])
{
	char * pchar = "test测试";
	
	int charLen = strlen(pchar);//计算char *数组大小,以字节为单位,一个汉字占两个字节
	
	int len = MultiByteToWideChar(CP_ACP,0,pchar,charLen,NULL,0);//计算多字节字符的大小,按字符计算。
	
	TCHAR *buf = new TCHAR[len + 1];//为宽字节字符数组申请空间,数组大小为按字节计算的多字节字符大小


	MultiByteToWideChar(CP_ACP,0,pchar,charLen,buf,len);//多字节编码转换成宽字节编码


	buf[len] = '\0';


	//将TCHAR数组转换为CString
	CString pWideChar;
	pWideChar.Append(buf);


	if(buf)
	{
		delete [] buf;
		buf = NULL;
	}


	return 0;
}

方法二:使用函数:A2T、A2W
#include <afx.h>

int _tmain(int argc, _TCHAR* argv[])
{
	char * pchar = "test测试";

	USES_CONVERSION;

	CString cstr = A2T(pchar);
	CString cstr1 = A2W(pchar); 

	return 0;
}

方法三:使用_T宏,将字符串转换为宽字符
书写代码使用TEXT("")或_T(""),文本在UNICODE和非UNICODE程序里都通用
#include <afx.h>

int _tmain(int argc, _TCHAR* argv[])
{
	CString cstr = _T("test测试");
	CString cstr1 = "test测试";

	return 0;
}

3、将char*转化为wchar_t*

#include <afx.h>
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;
}

4、将wchar_t*转化为char*

#include <afx.h>
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;
}

5、BSTR转换成char*

方法一,使用ConvertBSTRToString。例如:
char* lpszText2 = _com_util::ConvertBSTRToString(bstrText);
SysFreeString(bstrText); // 用完释放
delete[] lpszText2;  

方法二,使用_bstr_t的赋值运算符重载。例如:
_bstr_t b = bstrText;
char* lpszText2 = b; 

6、char*转换成BSTR

方法一,使用SysAllocString等API函数。例如:
BSTR bstrText = ::SysAllocString(L"test测试");

方法二,使用COleVariant或_variant_t。例如:
_variant_t strVar("test测试"); 
BSTR bstrText = strVar.bstrVal; 

方法三,使用_bstr_t。例如:
BSTR bstrText = _bstr_t("test测试"); 

方法四,使用CComBSTR。例如:
BSTR bstrText = CComBSTR("test测试");
或
CComBSTR bstr("test测试");
BSTR bstrText = bstr.m_str; 

方法五,使用ConvertStringToBSTR。例如:
char* lpszText = "test测试"; 
BSTR bstrText = _com_util::ConvertStringToBSTR(lpszText); 

7、CString转换成BSTR
通常是通过使用CStringT::AllocSysString来实现。例如:
CString str("test测试");
BSTR bstrText = str.AllocSysString();
SysFreeString(bstrText); // 用完释放

8、BSTR转换成CString
BSTR bstrText = ::SysAllocString(L"test测试"); 
CStringA str;
str.Empty();
str = bstrText; 
或
CStringA str(bstrText);