【问题标题】:bad_cast error with boost::locale::normalize带有 boost::locale::normalize 的 bad_cast 错误
【发布时间】:2020-07-17 00:27:01
【问题描述】:

我正在尝试将包含以 utf 编码的 txt 文件内容的字符串转换为使用 boost 的 unicode 字符串是 C++ 并在此之后对其进行规范化。不幸的是,我得到了 bad_cast 错误。任何人都可以帮忙吗? 代码:

convert_to_wstring(void *buffer, int length) {
    boost::locale::generator g;
    g.locale_cache_enabled(true);
    std::locale loc = g(boost::locale::util::get_system_locale());
    std::string buffer_char = static_cast<char *>(buffer);
    std::wstring result = boost::locale::conv::to_utf<wchar_t>(buffer_char, loc);
    result = boost::locale::normalize(result);
    result = boost::locale::fold_case(result);
    return result;
}

【问题讨论】:

    标签: c++ boost normalize boost-locale


    【解决方案1】:

    我正在研究它。问题是在某些语言环境中规范化字符串之前应该生成它,所以固定代码如下所示:

    convert_to_wstring(void *buffer, int length) {
        boost::locale::generator gen;
        gen.locale_cache_enabled(true);
        std::locale loc = gen(boost::locale::util::get_system_locale());
        std::string buffer_char = static_cast<char *>(buffer);
        std::wstring result = boost::locale::conv::to_utf<wchar_t>(buffer_char, loc);
        std::locale locale = gen("UTF-8");
        std::locale::global(locale);
        result = boost::locale::normalize(result);
        result = boost::locale::fold_case(result);
        return result;
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 2012-10-19
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 2023-03-21
      • 2016-02-10
      相关资源
      最近更新 更多