C++ & Boost: encode/decode UTF-8

前端 未结 4 2052
南方客
南方客 2020-12-01 06:29

I\'m trying to do a very simple task: take a unicode-aware wstring and convert it to a string, encoded as UTF8 bytes, and then the opposite way aro

4条回答
  •  死守一世寂寞
    2020-12-01 07:18

    Thanks everyone, but ultimately I resorted to http://utfcpp.sourceforge.net/ -- it's a header-only library that's very lightweight and easy to use. I'm sharing a demo code here, should anyone find it useful:

    inline void decode_utf8(const std::string& bytes, std::wstring& wstr)
    {
        utf8::utf8to32(bytes.begin(), bytes.end(), std::back_inserter(wstr));
    }
    inline void encode_utf8(const std::wstring& wstr, std::string& bytes)
    {
        utf8::utf32to8(wstr.begin(), wstr.end(), std::back_inserter(bytes));
    }
    

    Usage:

    wstring ws(L"\u05e9\u05dc\u05d5\u05dd");
    string s;
    encode_utf8(ws, s);
    

提交回复
热议问题