about iconv

Discussion in 'C Programming' started by yong, Mar 13, 2006.

  1. yong

    yong Guest

    I want to use iconv.h to convert some text to another charset.

    The code is below:

    ======
    #include <stdio.h>
    #include <stdlib.h>
    #include <iconv.h>

    int main()
    {
    iconv_t cd;
    char instr[]="汉字";
    char *inbuf;
    char *outbuf;
    unsigned int insize=7;
    unsigned int avail=10;
    unsigned int nconv;

    inbuf=instr;
    outbuf=malloc(10);

    cd=iconv_open("gbk","utf-8");
    if(cd==(iconv_t)-1)
    {
    printf("fail.\n");
    }
    nconv=iconv(cd,&inbuf,&insize,&outbuf,&avail);

    outbuf[5]='\0';
    printf("%s\n",outbuf);
    printf("nconv is: %d",nconv);

    return 0;
    }
    ======

    But after all things is done the buffer area "outbuf" is still empty.It
    doesn't output anything.Could someone give me some help?

    Thanks.
    yong, Mar 13, 2006
    #1
    1. Advertising

  2. yong wrote:
    > I want to use iconv.h to convert some text to another charset.


    AFAICT, <iconv.h> is not part of the C Standard, so you may find it
    difficult to get any help in this group.

    > The code is below:


    General comment on code below: horizontal spacing is cheap.

    > ======
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <iconv.h>
    >
    > int main()


    int main(void)

    Is better...

    > {
    > iconv_t cd;
    > char instr[]="汉字";
    > char *inbuf;
    > char *outbuf;
    > unsigned int insize=7;
    > unsigned int avail=10;
    > unsigned int nconv;
    >
    > inbuf=instr;
    > outbuf=malloc(10);


    You should check whether `malloc` succeeded (it'll return NULL if not).

    > cd=iconv_open("gbk","utf-8");
    > if(cd==(iconv_t)-1)
    > {
    > printf("fail.\n");
    > }
    > nconv=iconv(cd,&inbuf,&insize,&outbuf,&avail);
    >
    > outbuf[5]='\0';
    > printf("%s\n",outbuf);
    > printf("nconv is: %d",nconv);


    You did not terminate last `printf` with newline. Implementation is
    allowed to not output anything in this case.

    > return 0;
    > }
    > ======
    >
    > But after all things is done the buffer area "outbuf" is still empty.It
    > doesn't output anything.Could someone give me some help?


    You'll have to ask where <iconv.h> and functionality it provides is
    on-topic (I don't know, otherwise I'd tell you).

    --
    BR, Vladimir
    Vladimir S. Oka, Mar 13, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Strong IsOnlyWord

    How to fix the bug about iconv for python?

    Strong IsOnlyWord, Dec 26, 2005, in forum: Python
    Replies:
    1
    Views:
    602
    Strong IsOnlyWord
    Dec 26, 2005
  2. kp
    Replies:
    5
    Views:
    462
  3. Daniel Luis dos Santos

    iconv trouble

    Daniel Luis dos Santos, May 29, 2009, in forum: C++
    Replies:
    5
    Views:
    1,777
    James Kanze
    May 31, 2009
  4. Tim Ferrell

    Iconv.iconv and Windows XP

    Tim Ferrell, Oct 2, 2005, in forum: Ruby
    Replies:
    4
    Views:
    408
    nobuyoshi nakada
    Oct 4, 2005
  5. Krzysztof Cierpisz

    iconv "\n" (Iconv::InvalidCharacter)

    Krzysztof Cierpisz, Sep 8, 2009, in forum: Ruby
    Replies:
    0
    Views:
    171
    Krzysztof Cierpisz
    Sep 8, 2009
Loading...

Share This Page