Convert string from UTF-8 to ISO-8859-1

前端 未结 3 895
孤独总比滥情好
孤独总比滥情好 2021-01-05 08:06

I\'m trying to convert a UTF-8 string to a ISO-8859-1 char* for use in legacy code. The only way I\'m seeing to do this is with iconv.

I w

3条回答
  •  梦毁少年i
    2021-01-05 08:24

    Alfs suggestion implemented in C++11

    #include 
    #include 
    #include 
    #include 
    auto i = u8"H€llo Wørld";
    std::wstring_convert> utf8;
    auto wide = utf8.from_bytes(i);
    std::string out;
    out.reserve(wide.length());
    std::transform(wide.cbegin(), wide.cend(), std::back_inserter(out),
               [](const wchar_t c) { return (c <= 255) ? c : '?'; });
    // out now contains "H?llo W\xf8rld"
    

提交回复
热议问题