• 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‘;   //多字节字符以’′结束

    方法二:使用函数: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;
    }

    参考:

    http://msdn.microsoft.com/en-us/library/87zae4a3(v=vs.80).aspx

    WideCharToMultiByte:
    http://msdn.microsoft.com/en-us/library/windows/desktop/dd374130(v=vs.85).aspx

    MultiByteToWideChar:
    http://msdn.microsoft.com/en-us/library/windows/desktop/dd319072(v=vs.85).aspx

  • 相关阅读:
    web-火狐浏览器下载地址
    如何使用jmeter录制app脚本
    Jmeter参数化(jmter和badboy的配合使用)
    monkey常用命令详解(多参数化)
    monkey常用命令
    mysql数据库命令
    Linux命令
    循环语句的使用
    K8S学习笔记一:K8S的基础概念
    jmeter做尖峰测试(浪潮测试)
  • 原文地址:https://www.cnblogs.com/luxiaoxun/p/3454733.html
Copyright © 2020-2023  润新知