Using iconv translit to convert from UTF-8 to CP1251

自闭症网瘾萝莉.ら 提交于 2019-12-25 07:19:36

问题


I'am trying to convert string "" from UTF-8 to CP1251 via C++ library iconv.h using TRANSLIT and as a result I get string "a?", when expecting "aU".

When I use php script <?php echo iconv("UTF-8", "Windows-1251//TRANSLIT", "Ü");> on this computer, I get "aU" string as result.

Here's the code:

#include <cstdlib>
#include <iconv.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
using namespace std;

int IConvert(char *buf,char *outbuf, size_t len, const char *from, const char *to)
{
    iconv_t iconv_cd;
    if ((iconv_cd = iconv_open(to, from)) == (iconv_t) -1) {
        printf("Cannot open iconv from %s to %s\n", from, to);
        return 0;
    }

    char *inbuf = buf;
    size_t inlen = len;
    size_t outlen = len;
    size_t res = 0;

    while (inlen > 0 && outlen > 0) {
        res = iconv(iconv_cd, &inbuf, &inlen, &outbuf, &outlen);
        if (res == 0)
            break;

        if (res == (size_t) (-1)) {
            if (errno != EILSEQ && errno != EINVAL) {
                iconv_close(iconv_cd);
                *outbuf = '\0';
                printf("Erorr %s (%s)\n", strerror(errno), from);

                return 0;
            } else if (inbuf < outbuf) {
                iconv_close(iconv_cd);
                *outbuf = '\0';
                printf("Erorr %s (inbuf < outbuf)\n", strerror(errno));

                return 0;
            }
        }
        if (inlen > 0 && outlen > 0) {
            *outbuf++ = *inbuf++;
            inlen--;
            outlen--;
        }
    }
    iconv_close(iconv_cd);
    *outbuf = '\0';
    return 1;
}
int main(int argc, char** argv) {
        char* line = "\u00C0a\u00DC";
        char* from = (char*) malloc(strlen(from)+1);
        char* to = (char*) malloc(strlen(from)+1);
        strcpy(from, line);
        printf("%s\n", from);
        IConvert(from, to, strlen(from)+1, "UTF-8", "CP1251//TRANSLIT");
        printf("%s\n", to);
    return 0;
}

Any idea what problem could be?


回答1:


The solution is

setlocale(LC_ALL, "");

at the beginning of your program. Yes, the locale influences the transliteration. In a German locale Ü would be transliterated to UE not U.



来源:https://stackoverflow.com/questions/39162603/using-iconv-translit-to-convert-from-utf-8-to-cp1251

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!