我想在 C/C++ 中进行从 UTF-8 到 ISO 8859-15 的转换,而不包括额外的库。
我怎样才能做到这一点?
我找到了以下适用于 ISO 8859-1 的代码,但我不确定如何处理 ISO 8859-15 和 ISO 8859-1 之间的差异(https://en.wikipedia.org/wiki/ ISO/IEC_8859-15):
std::string UTF8toISO8859_1(const char * in) {
std::string out;
if (in == NULL)
return out;
unsigned int codepoint;
while (*in != 0) {
unsigned char ch = static_cast<unsigned char>(*in);
if (ch <= 0x7f)
codepoint = ch;
else if (ch <= 0xbf)
codepoint = (codepoint << 6) | (ch & 0x3f);
else if (ch <= 0xdf)
codepoint = ch & 0x1f;
else if (ch <= 0xef)
codepoint = ch & 0x0f;
else
codepoint = ch & 0x07;
++in;
if (((*in & 0xc0) != 0x80) && (codepoint <= 0x10ffff)) {
if (codepoint <= 255) {
out.append(1, static_cast<char>(codepoint));
}
else {
out.append("?");
}
}
}
return out;
}