简体   繁体   中英

bad_cast error with boost::locale::normalize

I am trying to convert string, which contains contents of txt file, that is encoded in utf, to unicode string is C++ using boost and normalize it after this. Unfortunately, I get bad_cast error. Can anyone help? Code:

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;
}

I worked on it. The problem was that before normalizing string in some locale it should be generated, so fixed code looks like this:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM