C++ 有什么简单的方法可以将ASCII转换为UTF8吗?(使用boost::locale)

C++ 有什么简单的方法可以将ASCII转换为UTF8吗?(使用boost::locale),c++,boost,utf-8,locale,C++,Boost,Utf 8,Locale,我正在尝试将一些字符串从ASCII转换为UTF8 我找了很多东西,但都找不到 所以我把这个编码了 std::string ASCII("ASCII string"); auto utf8 = boost::locale::conv::to_utf<char>(ansi, std::locale("en_US.UTF8")); std::字符串ASCII(“ASCII字符串”); 自动utf8=boost::locale::conv::to_utf(ansi,std::locale(

我正在尝试将一些字符串从ASCII转换为UTF8

我找了很多东西,但都找不到

所以我把这个编码了

std::string ASCII("ASCII string");
auto utf8 = boost::locale::conv::to_utf<char>(ansi, std::locale("en_US.UTF8"));
std::字符串ASCII(“ASCII字符串”);
自动utf8=boost::locale::conv::to_utf(ansi,std::locale(“en_US.utf8”);
是这样吗?在我的系统中,这不起作用

请帮帮我


谢谢。

如果您的意思是从您的系统语言环境转换为utf8或从utf8转换为utf8,我们将使用以下功能:

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);
}
static std::string fromlocate(const std::string&localeStr){
boost::locale::generator g;
g、 区域设置\缓存\已启用(true);
std::locale loc=g(boost::locale::util::get_system_locale());
返回boost::locale::conv::to_utf(localeStr,loc);
}
静态std::string toLocale(const std::string和utf8Str){
boost::locale::generator g;
g、 区域设置\缓存\已启用(true);
std::locale loc=g(boost::locale::util::get_system_locale());
返回boost::locale::conv::from_utf(utf8Str,loc);
}

如果“ansi”是指ASCII,那么它是UTF-8的严格子集。因此,不需要转换。你说的“来自ansi”是什么意思?ANSI编码可能意味着很多不同的东西。@usta是的,它意味着ASCII。我很抱歉。但转换是必需的,如果ASCII字符串是另一种语言,则需要convert@Simple它的意思是ASCII,对不起,我不知道well@PengChat:如果“ANSI”是指“ASCII”,则无需转换。UTF-8以与ASCII完全相同的方式表示代码点U+0000到U+007F(包括)(即ASCII)。如果有超出此范围的代码点,那么您不是在处理ASCII,您应该详细说明。很好的答案,正是我所需要的!谢谢这是针对哪个版本的boost编写的?