用boost locale库进行字符集转换的问题

来源:互联网 发布:禁止淘宝网页跳转到app 编辑:程序博客网 时间:2024/05/16 04:19


本想用 boost::locale::conv::to_utf<wchar_t> 写一个将 std::string 转换为 std::wstirng 的函数以达到简化接口形式,并且代码可以跨平台的目的。

然而 MSVC 2010  在 Windows 10上测试时遇到了问题。 以下是测试代码说明情况。


int _tmain(int argc, _TCHAR *argv[]){  try {    std::locale loc = boost::locale::generator().generate("" );    std::wstring a = L"1: Five Chinese words[白日依山尽]_by macro L";    std::wstring b = boost::locale::conv::to_utf<wchar_t>( "2: Five Chinese words[黄河入海流]_by boost to_utf", loc );     std::wstring c = L"3: Five Chinese words[欲穷千里目]_by macro L";     std::wcout.imbue( std::locale("") );    std::wcout << a <<  std::endl;    std::wcout << b <<  std::endl;    std::wcout << c <<  std::endl;    std::cout << "\n  charset of std::locale(\"\") is "               << std::use_facet<boost::locale::info>(loc).encoding()              << std::endl;  } catch ( std::exception &e ) {    std::cout << e.what() << std::endl;  }}

本期待输出四行,

1: Five Chinese words[白日依山尽]_by macro L2: Five Chinese words[黄河入海流]_by boost to_utf3: Five Chinese words[欲穷千里目]_by macro L  charset of std::locale("") is cp936

实际结果是:

1: Five Chinese words[白日依山尽]_by macro L2: Five Chinese words[  charset of std::locale("") is utf-8

第一行,正确;

第二行,从中文开始,没有显示;
第三行,没有显示;

第四行,显示系统默认字符集是 utf-8.    ( 实际输出的第三行)


可见从第二行的中文开始,wcout 就不能正常工作了。这与 boost 的 to_utf 函数说明不符。

第四行的结果“utf-8”说明了问题的原因。即由 boost::locale::generator().generate("" ) 产生的 std::locale loc,其中的字符集编码是 utf-8 而不是 Windows 系统中实际使用代码页 cp936 ( 对应字符集 GBK)。
但是,to_utf 只能接受由 boost::locale::generator 生成的 locale, 而不能直接接受 std::loale(""), 后者将导致 "bad_cast" 异常。因此,在windows下只能使用 to_utf 的第二种形式,即直接提供字符集名称来进行转换。
to_utf 在 Linux 下的行为则符合预期。

基于以上原因,不得不将 Windows 和 Linux 区别对待。在 windows 下使用字符集名称的方式,而在 Linux 下使用 boost 的标准写法。一个完整的例子如下:

#ifdef WIN32#include <windows.h>#endif#include <boost/locale.hpp>#include <boost/lexical_cast.hpp>#include <sstream>#include <iostream>#pragma comment(lib,"libboost_thread-vc100-mt-gd-1_55.lib")#pragma comment(lib,"libboost_system-vc100-mt-gd-1_55.lib")std::wstring toutf( std::string & src ){  #ifdef WIN32      static std::string codepage;    if ( codepage.empty() ) {      // 获得系统当前的代码页。对于中文Windows,       CPINFOEX  cpinfo;      GetCPInfoEx( CP_ACP, 0, &cpinfo );      cpinfo.CodePageName;      codepage = "CP" + boost::lexical_cast<std::string>(cpinfo.CodePage);    }        std::wstring dst = boost::locale::conv::to_utf<wchar_t>( src, codepage.c_str() );  #else    std::locale loc = boost::locale::generator().generate("");    std::wstring dst = boost::locale::conv::to_utf<wchar_t>( src, loc );  #endif    return dst;}int main(int argc, char *argv[]){  try {    std::locale loc = boost::locale::generator().generate("" );    std::wstring a = L"1: Five Chinese words[白日依山尽]_by macro L";    std::wstring b = toutf( std::string("2: Five Chinese words[黄河入海流]_by boost to_utf") );     std::wstring c = L"3: Five Chinese words[欲穷千里目]_by macro L";     std::wcout.imbue( std::locale("") );    std::wcout << a <<  std::endl;    std::wcout << b <<  std::endl;    std::wcout << c <<  std::endl;    std::cout << "\n  charset of std::locale(\"\") is "       << std::use_facet<boost::locale::info>(loc).encoding()      << std::endl;  } catch ( std::exception &e ) {    std::cout << e.what() << std::endl;  }}

输出的结果是:

1: Five Chinese words[白日依山尽]_by macro L2: Five Chinese words[黄河入海流]_by boost to_utf3: Five Chinese words[欲穷千里目]_by macro L  charset of std::locale("") is utf-8


0 0
原创粉丝点击