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


本想用 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 L
2: Five Chinese words[黄河入海流]_by boost to_utf
3: Five Chinese words[欲穷千里目]_by macro L

  charset of std::locale("") is cp936

实际结果是:

1: Five Chinese words[白日依山尽]_by macro L
2: 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 L
2: Five Chinese words[黄河入海流]_by boost to_utf
3: Five Chinese words[欲穷千里目]_by macro L

  charset of std::locale("") is utf-8


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值