UTF-8到拉丁语(ISO-8859-1)在C ++中的转换

时间:2013-09-02 08:51:39

标签: visual-c++ utf-8 character-encoding iso-8859-1

我想知道如何用C ++编写执行UTF-8到拉丁语(ISO-8859-1)转换的代码。

以下网站需要进行转换: http://www.unicodetools.com/unicode/utf8-to-latin-converter.php

插入值:úsername

提供结果:úsername

我有一段代码可以完成上一篇文章中的类似工作,但似乎没有转换字符串

int utf8_to_unicode(std::deque<int> &coded)
{
    int charcode = 0;
    int t = coded.front();
    coded.pop_front();
    if (t < 128)
    {
        return t;
    }
    int high_bit_mask = (1 << 6) -1;
    int high_bit_shift = 0;
    int total_bits = 0;
    const int other_bits = 6;
    while((t & 0xC0) == 0xC0)
    {
        t <<= 1;
        t &= 0xff;
        total_bits += 6;
        high_bit_mask >>= 1; 
        high_bit_shift++;
        charcode <<= other_bits;
        charcode |= coded.front() & ((1 << other_bits)-1);
        coded.pop_front();
    } 
    charcode |= ((t >> high_bit_shift) & high_bit_mask) << total_bits;
    return charcode;
}

请帮忙!

1 个答案:

答案 0 :(得分:0)

您需要libiconv中的iconv(3)功能。 iconv_t转换函数的第一个参数(一些iconv)应该在程序初始化时由iconv_open(3)获得,可能是

 ic = iconv_open("ISO-8859-1","UTF-8");

(其中ic是一些静态或全局iconv_t变量)。