简体   繁体   中英

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:

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

How to fix this error ? I searched in google but didn't find anything.

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". How can I add this encoding ?

You need to use the macro U_SUCCESS instead of just testing status . Negative error codes are warnings in 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 ). 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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