0

文字列をutf8からgb2312に変換するのに問題があります。私の変換関数は以下のとおりです

void convert(const char *from_charset,const char *to_charset, char *inptr, char *outptr)
{
    size_t inleft = strlen(inptr);
    size_t outleft = inleft;
    iconv_t cd;     /* conversion descriptor */

    if ((cd = iconv_open(to_charset, from_charset)) == (iconv_t)(-1)) 
    {
            fprintf(stderr, "Cannot open converter from %s to %s\n", from_charset, to_charset);
            exit(8);
    }

    /* return code of iconv() */
    int rc = iconv(cd, &inptr, &inleft, &outptr, &outleft);
    if (rc == -1) 
    {
            fprintf(stderr, "Error in converting characters\n");

            if(errno == E2BIG)
                    printf("errno == E2BIG\n");
            if(errno == EILSEQ)
                    printf("errno == EILSEQ\n");
            if(errno == EINVAL)
                    printf("errno == EINVAL\n");

            iconv_close(cd);
            exit(8);
    }
    iconv_close(cd);
}

これは私がそれをどのように使用したかの例です:

int len = 1000;
char *result = new char[len];
convert("UTF-8", "GB2312", some_string, result);

編集:私はほとんどの場合E2BIGエラーを受け取ります。

4

2 に答える 2

5

outleft は、着信文字列のサイズではなく、出力バッファーのサイズ (たとえば、1000 バイト) にする必要があります。

変換の際、文字列の長さは通常、その過程で変化し、その後までどのくらいの長さになるかはわかりません。E2BIG は、出力バッファーが十分に大きくないことを意味します。この場合、より多くの出力バッファー スペースを与える必要があります (データの一部が既に変換され、それに応じて渡された 4 つの変数が調整されていることに注意してください)。

于 2010-01-30T12:47:30.560 に答える
2

他の人が指摘しているように、E2BIG は、出力バッファが変換に十分な大きさではなく、outleft に間違った値を使用していたことを意味します。

しかし、あなたの機能には他にも問題がある可能性があることに気付きました。つまり、関数の動作方法では、呼び出し元は出力文字列に含まれるバイト数を知る方法がありません。convert() 関数は、出力バッファーをヌルで終了することも、outptr に書き込んだバイト数を呼び出し元に伝える手段もありません。

nul で終わる文字列を処理したい場合 (そして、入力文字列が nul で終わっているので、それをやりたいと思われる場合)、次のアプローチの方がはるかに優れていることに気付くかもしれません。


char *
convert (const char *from_charset, const char *to_charset, const char *input)
{
 size_t inleft, outleft, converted = 0;
 char *output, *outbuf, *tmp;
 const char *inbuf;
 size_t outlen;
 iconv_t cd;

 if ((cd = iconv_open (to_charset, from_charset)) == (iconv_t) -1)
  return NULL;

 inleft = strlen (input);
 inbuf = input;

 /* we'll start off allocating an output buffer which is the same size
  * as our input buffer. */
 outlen = inleft;

 /* we allocate 4 bytes more than what we need for nul-termination... */
 if (!(output = malloc (outlen + 4))) {
  iconv_close (cd);
  return NULL;
 }

 do {
  errno = 0;
  outbuf = output + converted;
  outleft = outlen - converted;

  converted = iconv (cd, (char **) &inbuf, &inleft, &outbuf, &outleft);
  if (converted != (size_t) -1 || errno == EINVAL) {
   /*
    * EINVAL  An  incomplete  multibyte sequence has been encoun­-
    *         tered in the input.
    *
    * We'll just truncate it and ignore it.
    */
   break;
  }

  if (errno != E2BIG) {
   /*
    * EILSEQ An invalid multibyte sequence has been  encountered
    *        in the input.
    *
    * Bad input, we can't really recover from this. 
    */
   iconv_close (cd);
   free (output);
   return NULL;
  }

  /*
   * E2BIG   There is not sufficient room at *outbuf.
   *
   * We just need to grow our outbuffer and try again.
   */

  converted = outbuf - out;
  outlen += inleft * 2 + 8;

  if (!(tmp = realloc (output, outlen + 4))) {
   iconv_close (cd);
   free (output);
   return NULL;
  }

  output = tmp;
  outbuf = output + converted;
 } while (1);

 /* flush the iconv conversion */
 iconv (cd, NULL, NULL, &outbuf, &outleft);
 iconv_close (cd);

 /* Note: not all charsets can be nul-terminated with a single
  * nul byte. UCS2, for example, needs 2 nul bytes and UCS4
  * needs 4. I hope that 4 nul bytes is enough to terminate all
  * multibyte charsets? */

 /* nul-terminate the string */
 memset (outbuf, 0, 4);

 return output;
}
于 2010-01-30T15:31:44.237 に答える