Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any easy methods to convert from ASCII to UTF8? (using boost::locale)

I'm trying convert some strings from ASCII to UTF8.

And, i searched many things, but i can't found it.

So i coded this.

std::string ASCII("ASCII string");
auto utf8 = boost::locale::conv::to_utf<char>(ansi, std::locale("en_US.UTF8"));

Is this right? on my system this is dosen't work.

Please, help me.

Thanks.

like image 725
Joy Hyuk Lee Avatar asked Oct 27 '25 09:10

Joy Hyuk Lee


1 Answers

If you mean conversion from your system locale to utf8 or vice versa, we use the following which is working well:

static std::string fromLocale(const std::string &localeStr){
    boost::locale::generator g;
    g.locale_cache_enabled(true);
    std::locale loc = g(boost::locale::util::get_system_locale());
    return boost::locale::conv::to_utf<char>(localeStr,loc);
}

static std::string toLocale(const std::string &utf8Str){
    boost::locale::generator g;
    g.locale_cache_enabled(true);
    std::locale loc = g(boost::locale::util::get_system_locale());
    return boost::locale::conv::from_utf<char>(utf8Str,loc);
}
like image 112
choosyg Avatar answered Oct 30 '25 00:10

choosyg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!