One way could be to use the Posix libiconv library. On Linux, the functions needed (iconv_open, iconv and iconv_close) are even included in libc so no extra linkage is needed there. On your old machines you may need to install libiconv but I doubt it.
Converting may be as simple as this:
#include <iconv.h> #include <cerrno> #include <cstring> #include <iostream> #include <iterator> #include <stdexcept> #include <string> // A wrapper for the iconv functions class Conv { public: // Open a conversion descriptor for the two selected character sets Conv(const char* to, const char* from) : cd(iconv_open(to, from)) { if(cd == reinterpret_cast<iconv_t>(-1)) throw std::runtime_error(std::strerror(errno)); } Conv(const Conv&) = delete; ~Conv() { iconv_close(cd); } // the actual conversion function std::string convert(const std::string& in) { const char* inbuf = in.c_str(); size_t inbytesleft = in.size(); // make the "out" buffer big to fit whatever we throw at it and set pointers std::string out(inbytesleft * 6, '\0'); char* outbuf = out.data(); size_t outbytesleft = out.size(); // the const_cast shouldn't be needed but my "iconv" function declares it // "char**" not "const char**" size_t non_rev_converted = iconv(cd, const_cast<char**>(&inbuf), &inbytesleft, &outbuf, &outbytesleft); if(non_rev_converted == static_cast<size_t>(-1)) { // here you can add misc handling like replacing erroneous chars // and continue converting etc. // I'll just throw... throw std::runtime_error(std::strerror(errno)); } // shrink to keep only what we converted out.resize(outbuf - out.data()); return out; } private: iconv_t cd; }; int main() { Conv cvt("UTF-8", "ISO-8859-7"); // create a string from the ISO-8859-7 data unsigned char data[]{0xcf, 0xcb, 0xc1}; std::string iso88597_str(std::begin(data), std::end(data)); auto utf8 = cvt.convert(iso88597_str); std::cout << utf8 << '\n'; }
Output (in UTF-8):
ΟΛΑ
Using this you can create a mapping table, from ISO-8859-7 to UTF-8, that you include in your project instead of iconv:
Demo
libiconvcount? It's so common that the functions are even included in gnu'slibcso you don't even have to link with extra libraries on linux for example.