3

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.

Joy Hyuk Lee
  • 756
  • 11
  • 22
  • 2
    If by "ansi" you mean ASCII, then it is a strict subset of UTF-8. As such, no conversion is required. – usta Aug 14 '14 at 15:24
  • What do you mean "from ansi"? ANSI encoding can mean a lot of different things. – Simple Aug 14 '14 at 15:25
  • @usta Yes, it is mean ASCII. i'm sorry. but conversion is required, if ASCII string is another language, it is need to convert – Joy Hyuk Lee Aug 14 '14 at 16:17
  • @Simple It means ASCII, I'm sorry, I do not know well – Joy Hyuk Lee Aug 14 '14 at 16:25
  • 1
    @PengChat: If by "ANSI" you mean "ASCII", then no conversion is required. UTF-8 represents code points U+0000 through U+007F (inclusive) (i.e. ASCII) exactly the same way as ASCII does. If there are code points outside this range, then you're not dealing with ASCII, and you should elaborate. – Robert Allan Hennigan Leahy Aug 14 '14 at 16:30
  • @Lee ASCII cannot be "another language" - it is strictly [this set](http://en.wikipedia.org/wiki/File:ASCII_Code_Chart-Quick_ref_card.jpg) of characters, nothing more, nothing less. Now it is clear what you wanted to do instead, and I'm glad you now have a working solution. – usta Aug 16 '14 at 17:19
  • @usta Thanks a lot. Now i have a working solution :) – Joy Hyuk Lee Aug 17 '14 at 13:28

1 Answers1

11

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);
}
choosyg
  • 774
  • 7
  • 23