用boost locale库进行字符集转换的问题
来源:互联网 发布:禁止淘宝网页跳转到app 编辑:程序博客网 时间:2024/05/16 04:19
然而 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
- 用boost locale库进行字符集转换的问题
- 使用 Boost 的Locale 進行字碼轉換
- 使用 Boost 的Locale 進行字碼轉換
- boost库学习随记五 Boost.Locale 之字符转换 gbk utf8 big5 string wstring等
- boost::locale::conv:: 字符编码转换
- Boost.Locale 本地化库 ...待续...
- 使用iconv库进行字符集转换
- 用boost::lexical_cast进行数值转换
- 用boost::lexical_cast进行数值转换
- 在Visual Studio平台上的应用libiconv库进行字符集转换的范例
- 字符集编码转换问题
- Hadoop字符集转换问题
- PHP字符集转换问题
- 字符集本地化(locale)与输入法系列讲座-----(4) Ubuntu Locale配置问题根源解决之道
- linux locale和字符集
- vc6转换到vs2008 fopen出现字符集转换的问题
- vc6转换到vs2008 fopen出现字符集转换的问题
- 使用Boost进行数据转换
- Win7 C盘越来越小怎么办?
- [Objective-C]C语言特性(函数,变量,编译指令,指针,块)
- Unity3d资源自动更新
- INFA安装时自动生成的文本
- 国务院:《促进大数据发展行动纲要》-全文
- 用boost locale库进行字符集转换的问题
- LR 脚本在init中参数化的问题
- 调取手机设置模式里的震动或铃声 Android
- 获取进程能打开的最大文件数量【OPEN_MAX】
- leetcode Single Number III 也是位操作
- Linux网络编程:基于TCP的程序开发回顾篇
- Matlab 生成mexwin64文件时遇到的问题
- 【ThinkPHP】ThinkPHP对Mysql数据库的增删改查,volist标签附带条件判断的用法
- HDU4355-Party All the Time-三分法(模板)-2012 Multi-University Training Contest 6