I want to store utf8
characters in my std::strings
. For that I used boost::locale
conversion routines.
In my first test everything works as expected:
#include <boost/locale.hpp>
std::string utf8_string = boost::locale::conv::to_utf<char>("Grüssen", "ISO-8859-15");
std::string normal_string = boost::locale::conv::from_utf(utf8_string, "ISO-8859-15");
The expected Result is:
utf8_string = "Grüssen"
normal_string = "Grüssen"
To get rid of passing "ISO-8859-15" as string I tried to use std::locale
instead.
// Create system default locale
boost::locale::generator gen;
std::locale loc=gen("ISO8859-15");
std::locale::global(loc);
// This is needed to prevent C library to
// convert strings to narrow
// instead of C++ on some platforms
std::ios_base::sync_with_stdio(false);
std::string utf8_string = boost::locale::conv::to_utf<char>("Grüssen", std::locale());
std::string normal_string = boost::locale::conv::from_utf(utf8_string, std::locale());
But the result is not as expected:
utf8_string = "Gr|ssen"
normal_string = "Gr|ssen"
What's wrong with my use of using std::locale
and generator?
(Compiler VC2015, charset multibyte)