Reputation: 448
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;
}
Upvotes: 1
Views: 425
Reputation: 448
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;
}
Upvotes: 1