【问题标题】:UTF-8 to Latin(ISO-8859-1) Conversion in C++UTF-8 到拉丁文(ISO-8859-1)在 C++ 中的转换
【发布时间】:2013-09-05 07:40:13
【问题描述】:

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

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

插入值:ú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;
}

请帮忙!

【问题讨论】:

  • iconv 可能有用。
  • @BasileStarynkevitch 我正在寻找一种在源代码中对这种转换进行硬编码的解决方案
  • Iconv 也是一个库libiconv。我不确定您在源代码中对转换进行硬编码是什么意思。您不能配置用于源代码的文本编辑器(例如emacs)以将其保存为所需的编码吗?
  • @BasileStarynkevitch 请参阅上面的代码了解我要查找的内容

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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2012-12-22
    • 2016-07-18
    • 1970-01-01
    • 2019-02-21
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 2012-03-09
    相关资源
    最近更新 更多