VC++中字符串编码的转换

在以前VC++6.0中默认的字符集是多字节字符集(MBCS:Multi-Byte Character Set),而VS2005及以后默认的字符集是Unicode,这样导致以前在VC6.0中非常简单实用的各类字符操作和函数在VS2010环境下运行时会报各种各样的错误。

字符集可以通过工程属性修改:“工程-属性-字符集”。

CString在Unicode和多字节字符集下的区别:CString 是基于 TCHAR 数据类型的。如果为程序的生成定义了符号 _UNICODE,则会将 TCHAR 定义为 wchar_t 类型(一个 16 位的字符编码类型);否则,会将它定义为 char(普通的 8 位字符编码)。于是,在 Unicode 下,CString 由 16 位字符组成。如果没有 Unicode,它们则由 char 类型的字符组成(来自MSDN)。

以下是CString在Visual C++ .NET 2010环境中Unicode字符集下CString和char *之间相互转换的几种方法,其实也就是Unicode字符集与MBCS字符集转换。

1.Unicode下CString转换为char *

方法一: 使用API:WideCharToMultiByte进行转换

复制代码
CString str = _T("你好,世界!Hello,World");
//注意:以下n和len的值大小不同,n是按字符计算的,len是按字节计算的
int n = str.GetLength();  
//获取宽字节字符的大小,大小是按字节计算的
int len = WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),NULL,0,NULL,NULL);
//为多字节字符数组申请空间,数组大小为按字节计算的宽字节字节大小
char * pFileName = new char[len+1];   //以字节为单位
//宽字节编码转换成多字节编码
WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),pFileName,len,NULL,NULL);
pFileName[len+1] = ‘\0‘;   //多字节字符以’\0′结束
复制代码

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

CString str = _T("你好,世界!Hello,World");
//声明标识符
USES_CONVERSION;
//调用函数,T2A和W2A均支持ATL和MFC中的字符转换
char * pFileName = T2A(str);   
//char * pFileName = W2A(str); //也可实现转换

2、Unicode下char *转换为CString

方法一:使用API:MultiByteToWideChar进行转换

复制代码
char * pFileName = "你好,世界!Hello,World";
//计算char *数组大小,以字节为单位,一个汉字占两个字节
int charLen = strlen(pFileName);
//计算多字节字符的大小,按字符计算。
int len = MultiByteToWideChar(CP_ACP,0,pFileName,charLen,NULL,0);
//为宽字节字符数组申请空间,数组大小为按字节计算的多字节字符大小
TCHAR *buf = new TCHAR[len + 1];
//多字节编码转换成宽字节编码
MultiByteToWideChar(CP_ACP,0,pFileName,charLen,buf,len);
buf[len] = ‘\0‘; //添加字符串结尾,注意不是len+1
//将TCHAR数组转换为CString
CString pWideChar;
pWideChar.Append(buf);
//删除缓冲区
delete []buf;
复制代码

方法二:使用函数:A2T、A2W

char * pFileName = "你好,世界!Hello,World"; 
USES_CONVERSION;
CString s = A2T(pFileName);
//CString s = A2W(pFileName);

下面是在网上看到的转换代码,注意函数MultiByteToWideChar()和WideCharToMultiByte()第四个参数传入-1时表示第三个参数传入的字符串是null结尾的(null-terminated),这时候返回的字节数(字符数)就包含了null,详情看MSDN。

复制代码
#include "stdafx.h"

#include <windows.h>
#include <iostream>
#include <vector>
#include <atlstr.h>

using namespace std;

std::wstring UT2WC(const char* buf)
{
    int len = MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);
    std::vector<wchar_t> unicode(len);
    MultiByteToWideChar(CP_UTF8, 0, buf, -1, &unicode[0], len);
    return std::wstring(&unicode[0]);
}

std::string WC2UT(const wchar_t* buf)
{
    int len = WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);
    std::vector<char> utf8(len);
    WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);
    return std::string(&utf8[0]);
}

std::wstring MB2WC(const char* buf)
{
    int len = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
    std::vector<wchar_t> unicode(len);
    MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);
    return std::wstring(&unicode[0]);
}

std::string WC2MB(const wchar_t* buf)
{
    int len = WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL);
    std::vector<char> utf8(len);
    WideCharToMultiByte(CP_ACP, 0, buf, -1, &utf8[0], len, NULL, NULL);
    return std::string(&utf8[0]);
}


int main()
{
    setlocale(LC_ALL, "");
    CString str = "UNICODE转换成UTF-8";
    //cout << WC2UT(str).c_str() << endl; //Unicode下
    BSTR bstr = str.AllocSysString();
    cout << WC2UT(bstr).c_str() << endl; //多字符集下/Unicode下
    
    std::string s = WC2UT(bstr);
    SysFreeString(bstr);
    std::wstring ws = UT2WC(s.c_str());
    wcout<< ws.c_str() << endl;

    const wchar_t* s1 = L"UNICODE转换成UTF-8";
    cout << WC2UT(s1).c_str() << endl;

    const char* s2 = "ANSI转换成UNICODE";
    wcout << MB2WC(s2).c_str() << endl;
    
    const wchar_t* s3 = L"UNICODE转换成ANSI";
    cout << WC2MB(s3).c_str() << endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值