4

I'm having trouble using boost to convert between different string encoding. After reading this, I tried doing this:

boost::locale::generator gen;
std::locale loc = gen.generate("");// encoding local to the computer.
//std::locale loc = gen.generate("en_US.UTF-8"); // tried this too
//std::locale loc = gen.generate("en_US.UTF-8"); //doesn't work either
std::string someString = "test me";
std::string output = boost::locale::conv::to_utf<char>(someString, loc);

But it always fails with a std::bad_cast when it tries use_facet() inside to_utf.

This would suggest that the boost locale::info facet isn't available for the locale being generated. But I tried a few different generations with no luck, and I expect the system default to always work, so I must be doing something wrong else where.

Any ideas?

4

1 回答 1

2
std::locale loc("");
std::locale conv_loc = boost::locale::util::create_info(loc, loc.name());
std::string output = boost::locale::conv::to_utf<char>(someString, conv_loc);
于 2012-12-10T19:46:06.980 回答