0

Solaris 8中,函数家族似乎iconv*()被破坏了,仅支持单字节字符集和 之间的转换UTF-8,可以使用以下代码示例进行验证:

#include <stdio.h>
#include <errno.h>
#include <iconv.h>

#if defined(__sun) && defined(__SVR4)
#define CP1251 "ansi-1251"
#define ISO_8859_5 "ISO8859-5"
#else
#define CP1251 "CP1251"
#define ISO_8859_5 "ISO-8859-5"
#endif

void iconv_open_debug(const char *, const char *);

int main() {
    iconv_open_debug(CP1251, CP1251);
    iconv_open_debug(CP1251, ISO_8859_5);
    iconv_open_debug(CP1251, "KOI8-R");
    iconv_open_debug(CP1251, "UTF-8");
    iconv_open_debug(CP1251, "WCHAR_T");

    iconv_open_debug(ISO_8859_5, CP1251);
    iconv_open_debug(ISO_8859_5, ISO_8859_5);
    iconv_open_debug(ISO_8859_5, "KOI8-R");
    iconv_open_debug(ISO_8859_5, "UTF-8");
    iconv_open_debug(ISO_8859_5, "WCHAR_T");

    iconv_open_debug("KOI8-R", CP1251);
    iconv_open_debug("KOI8-R", ISO_8859_5);
    iconv_open_debug("KOI8-R", "KOI8-R");
    iconv_open_debug("KOI8-R", "UTF-8");
    iconv_open_debug("KOI8-R", "WCHAR_T");

    iconv_open_debug("UTF-8", CP1251);
    iconv_open_debug("UTF-8", ISO_8859_5);
    iconv_open_debug("UTF-8", "KOI8-R");
    iconv_open_debug("UTF-8", "UTF-8");
    iconv_open_debug("UTF-8", "WCHAR_T");

    iconv_open_debug("WCHAR_T", CP1251);
    iconv_open_debug("WCHAR_T", ISO_8859_5);
    iconv_open_debug("WCHAR_T", "KOI8-R");
    iconv_open_debug("WCHAR_T", "UTF-8");
    iconv_open_debug("WCHAR_T", "WCHAR_T");

    return 0;
}

void iconv_open_debug(const char *from, const char *to) {
    errno = 0;
    if (iconv_open(to, from) == (iconv_t) -1) {
        fprintf(stderr, "iconv_open(\"%s\", \"%s\") FAIL: errno = %d\n", to, from, errno);
        perror("iconv_open()");
    } else {
        fprintf(stdout, "iconv_open(\"%s\", \"%s\") PASS\n", to, from);
    }
}

只打印

iconv_open("UTF-8", "ansi-1251") PASS
iconv_open("UTF-8", "ISO8859-5") PASS
iconv_open("UTF-8", "KOI8-R") PASS
iconv_open("ansi-1251", "UTF-8") PASS
iconv_open("ISO8859-5", "UTF-8") PASS
iconv_open("KOI8-R", "UTF-8") PASS

到标准输出并返回EINVAL其他对。请注意,甚至不支持转换为相同的字符集(例如 UTF-8 -> UTF-8)。

问题

  1. 任何人都可以参考描述Solaris版本限制的文档iconv.h吗?
  2. 如何在wchar_t*不依赖GNU libiconv的情况下将 a 转换为单字节或多字节字符串?wcstombs() 很好,只是它依赖于当前语言环境的字符集,而我希望使用特定字符集将宽字符串转换为常规字符串,可能与默认字符集不同。
4

1 回答 1

0

运行sdtconvtool显示支持大多数旧代码页。

在使用 重新运行相同的实用程序后truss -u libc::iconv_open,我了解到从一种单字节编码转换为另一种单字节编码是分两步完成的,中间转换为UTF-8.

说到从 的转换"WCHAR_T"iconv(3)也确实支持它,但"UCS-4"应该用作源字符集名称,因为sizeof(wchar_t)在 Solaris 上是 4(对于 x86 和 SPARC)。

于 2015-11-20T08:15:09.220 回答