about iconv

Y

yong

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

Vladimir S. Oka

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

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top