简体   繁体   English

Ubuntu上的库ICU不想从Unicode转换为Windows-1251

[英]Library ICU on Ubuntu doesn't want to convert from Unicode to windows-1251

I am using ICU library and I need to convert from Unicode to windows-1251, I wrote this simple code: 我正在使用ICU库,我需要从Unicode转换为Windows-1251,我编写了以下简单代码:

#include <unicode/unistr.h>
#include <unicode/ucnv.h>

int main(int argc, char** argv)
{
    UErrorCode status = U_ZERO_ERROR;
    UConverter *pConvert = ucnv_open("windows-1251", &status);
    if (status)
    {
        printf("Failed to obtain char set converter: %d\r\n", status);
        return false;
    }
}

I always get this error: "Failed to obtain char set ....." during creation of the UConverter object. 在创建UConverter对象的过程中,总是出现以下错误:“无法获取字符集.....”。

How to fix this error ? 如何解决此错误? I searched in google but didn't find anything. 我在Google中搜索,但未找到任何内容。

I used this code to get a list of all available converters contaied in the alias file: 我使用以下代码来获取别名文件中包含的所有可用转换器的列表:

for(int i = 0; i < ucnv_countAvailable(); ++i)
    {
        printf("   %s  \n", ucnv_getAvailableName(i));
    }

I didn't find in this list "windows-1251". 我在此列表中未找到“ windows-1251”。 How can I add this encoding ? 如何添加此编码?

You need to use the macro U_SUCCESS instead of just testing status . 您需要使用宏U_SUCCESS而不是仅测试status Negative error codes are warnings in ICU: 负错误代码是ICU中的警告:

typedef enum UErrorCode {
  // ...
  U_AMBIGUOUS_ALIAS_WARNING = -122

This works just fine: 这很好用:

auto converter = ucnv_open("windows-1251", &error);
if (U_SUCCESS(error))
{
  printf("Success! %s\n", ucnv_getName(converter, &error));
} 

And prints out: 并打印出:

Success! ibm-5347_P100-1998

The reason you get the "ambiguous" warning is because "windows-1251" is an alias for more than one canonical name ( ibm-5347_P100-1998 and ibm-1251_P100-1995 ). 之所以收到“模糊”警告,是因为“ windows-1251”是多个规范名称的别名( ibm-5347_P100-1998ibm-1251_P100-1995 )。 You can see this by updating your sample with the "alias" functions: 您可以通过使用“别名”功能更新示例来查看此信息:

int main()
{
  UErrorCode error{ U_ZERO_ERROR };
  const auto n = ucnv_countAvailable();
  for (int i = 0; i < n; ++i)
  {
    auto s = ucnv_getAvailableName(i);
    const auto m = ucnv_countAliases(s, &error);
    if (U_SUCCESS(error))
    {
      for (int j = 0; j < m; ++j)
      {
        auto a = ucnv_getAlias(s, j, &error);
        if (U_SUCCESS(error) && strstr(a, "windows-1251"))
          printf("%s --> %s\n", s, a);
      }
    }
  }
}

(Delete the strstr call to see the very long list of all names / aliases). (删除strstr调用以查看所有名称/别名的很长的列表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM