使用iconv translit将UTF-8转换为CP1251 [英] Using iconv translit to convert from UTF-8 to CP1251

查看:176
本文介绍了使用iconv translit将UTF-8转换为CP1251的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串 UTF-8 转换为 iconv.h > CP1251 ,结果得到字符串 a?,当期望 aU 时。

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".

当我使用php脚本<?php echo iconv( UTF-8, Windows-1251 // TRANSLIT,Ü);> 在此计算机上,我得到 aU 字符串。

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

这里是代码:

#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?

推荐答案

解决方案

setlocale(LC_ALL, "");

在程序开始时。是的,地区会影响音译。在德国语言环境中,Ü会音译为UE而不是U。

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

这篇关于使用iconv translit将UTF-8转换为CP1251的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆