author: love02xp
title: Unicode编码下字符串相互转换
date: 2016-07-10 15:45:20
category: [编程代码]
tags: [学习,编程]
#####[阅读原文](http://blog.csdn.net/one_in_one/article/details/26346137)
1、Unicode编码下CString转为char*
<!--more-->
方法一:使用API:WideCharToMultiByte进行转换
[cpp] view plain copy
<span style="font-size:18px;">#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;
}</span>
方法二:使用函数:T2A、W2A
[cpp] view plain copy
<span style="font-size:18px;">#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;
}</span>
2、Unicode下char *转换为CString
方法一:使用API:MultiByteToWideChar进行转换
[cpp] view plain copy
<span style="font-size:18px;">#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;
}</span>
方法二:使用函数:A2T、A2W
[cpp] view plain copy
<span style="font-size:18px;">#include <afx.h>
int _tmain(int argc, _TCHAR* argv[])
{
char * pchar = "test测试";
USES_CONVERSION;
CString cstr = A2T(pchar);
CString cstr1 = A2W(pchar);
return 0;
}</span>
方法三:使用_T宏,将字符串转换为宽字符
书写代码使用TEXT("")或_T(""),文本在UNICODE和非UNICODE程序里都通用
[cpp] view plain copy
<span style="font-size:18px;">#include <afx.h>
int _tmain(int argc, _TCHAR* argv[])
{
CString cstr = _T("test测试");
CString cstr1 = "test测试";
return 0;
}</span>
3、将char*转化为wchar_t*
[cpp] view plain copy
<span style="font-size:18px;">#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;
}</span>
4、将wchar_t*转化为char*
[cpp] view plain copy
#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*
[cpp] view plain copy
<span style="font-size:18px;">方法一,使用ConvertBSTRToString。例如:
char* lpszText2 = _com_util::ConvertBSTRToString(bstrText);
SysFreeString(bstrText); // 用完释放
delete[] lpszText2;
方法二,使用_bstr_t的赋值运算符重载。例如:
_bstr_t b = bstrText;
char* lpszText2 = b; </span>
6、char*转换成BSTR
[cpp] view plain copy
方法一,使用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
[cpp] view plain copy
通常是通过使用CStringT::AllocSysString来实现。例如:
CString str("test测试");
BSTR bstrText = str.AllocSysString();
SysFreeString(bstrText); // 用完释放
8、BSTR转换成CString
[cpp] view plain copy
<span style="font-size:14px;">BSTR bstrText = ::SysAllocString(L"test测试");
CStringA str;
str.Empty();
str = bstrText;
或
CStringA str(bstrText);</span>