Convert ISO-8859-1 strings to UTF-8 in C/C++

后端 未结 6 897
礼貌的吻别
礼貌的吻别 2020-12-05 05:27

You would think this would be readily available, but I\'m having a hard time finding a simple library function that will convert a C or C++ string from ISO-8859-1 coding to

6条回答
  •  孤街浪徒
    2020-12-05 05:37

    To c++ i use this:

    std::string iso_8859_1_to_utf8(std::string &str)
    {
        string strOut;
        for (std::string::iterator it = str.begin(); it != str.end(); ++it)
        {
            uint8_t ch = *it;
            if (ch < 0x80) {
                strOut.push_back(ch);
            }
            else {
                strOut.push_back(0xc0 | ch >> 6);
                strOut.push_back(0x80 | (ch & 0x3f));
            }
        }
        return strOut;
    }
    

提交回复
热议问题