about iconv


 
Thread Tools Search this Thread
Top Forums Programming about iconv
# 1  
Old 03-13-2006
about iconv

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

The code is below:

Code:
#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.
# 2  
Old 03-13-2006
The offset of outbuf after iconv() will be advanced, so you need to do some pointer arithmetic to go back by the number of advanced output bytes, or maintain another pointer to the output buffer before iconv() and use that.

For example, this works on my system:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>

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

        inbuf=instr;
        outbuf=(char*)malloc(outbufsize);
		memset(outbuf, '\0', outbufsize);

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

        printf("%s\n", outbuf);
        printf("nconv is: %d\n", nconv);
		free(outbuf);

        return 0;
}

Seems like the return value of iconv() is not too meaningful except for checking error condition. Most programs I have seen using iconv() just throw it away.
# 3  
Old 03-14-2006
I modified my code like yours and it begins to work now.

Thanks a lot!

My modified code is pasted below: Smilie

Code:
#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>
#include <string.h>

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

        inbuf=instr;
        outbuf=(char *)malloc(outputbufsize);
	outptr=outbuf;
	memset(outbuf,'\0',outputbufsize);

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

        //outbuf[5]='\0';
        printf("%s\n",outbuf);
        printf("%s\n","aa");

        return 0;
}


Last edited by yong; 03-14-2006 at 08:29 PM.. Reason: small change
# 4  
Old 03-14-2006
Quote:
outbuf[5]='\0';
You don't need this, because memset at the beginning will fill the malloc()ed buffer with \0 bytes.
# 5  
Old 03-14-2006
Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Explain iconv command

I have a requirement to remove all non-ascii characters from a fixed length file. I used the below command which is removing special characters but somehow the total record length is being truncated to one space less. If it is a multi-byte string then many characters at the end are being truncated.... (8 Replies)
Discussion started by: eskay
8 Replies

2. UNIX for Advanced & Expert Users

Iconv on large files

Hi All, I am using iconv to convert huge files. the process is getting killed. I tried the option in below link https://www.unix.com/shell-programming-and-scripting/258825-iconv-large-files.html i.e iconv -f UCS-2 -t UTF-8 < inputfile.txt > outputfile.txt However, the process still gets... (4 Replies)
Discussion started by: tostay2003
4 Replies

3. Shell Programming and Scripting

Iconv for large files

Hi I am using iconv command to convert the encoding of files. Below is the command used: iconv -f UCS-2 -t UTF-8 inputfile.txt> outputfile.txt The command is working fine for files less than 2GB. When I try converting the files of more than 2GB size I get an error as 'Can't open file'. I... (2 Replies)
Discussion started by: vrcr
2 Replies

4. Shell Programming and Scripting

Help with command iconv

I need to convert a utf16 file to utf8. When i use the iconv command to do so it gives an error saying invalid function. When I ran the iconv -l function it did not list the utf16 and utf8 as part of its internal table. Is there anyway I can add these encodings in the library? Is there any other... (3 Replies)
Discussion started by: gaun
3 Replies

5. UNIX for Dummies Questions & Answers

Help with iconv command

Hi , I am using iconv command to convert a file in UTF-16 format to UTF-8 format. This command will work for few files but for some showing an error as bad input character. But if i copy the contents of the file for which it is showing "bad input character" to a new file and perform the... (2 Replies)
Discussion started by: Shruthi8818
2 Replies

6. Shell Programming and Scripting

Help with iconv command

Hi , I am using iconv command to convert a file in UTF-16 format to UTF-8 format. This command will work for few files but for some showing an error as bad input character. But if i copy the contents of the file for which it is showing "bad input character" to a new file and perform the... (0 Replies)
Discussion started by: Shruthi8818
0 Replies

7. Shell Programming and Scripting

initial setup for iconv

hi I am trying iconv on my linux machine for conversion of RUSSIAN to ENGLISH, but i am not able to get exact result. i want to know what initial setting in linux machine we need to do to get desired output I created sample russian file using google translate in CP866 endcoding and full... (5 Replies)
Discussion started by: peeyushgehlot
5 Replies

8. Shell Programming and Scripting

calling iconv from within a script

hi, i am new to unix shell scripting and really desparate about this, so i appreciate any help on this matter. it maybe something obvious as well. two different scripts, the first one is working fine, the second one is not: #!/bin/bash if then echo "usage: $0 <Input file> <Output... (1 Reply)
Discussion started by: ssulger
1 Replies

9. UNIX for Advanced & Expert Users

iconv and xmllint

Here is my question, volume of records processed : 5M ( approx ) Its basically very simple operation that am trying to do and I had achieved the output that am interested. What am looking for really is to improve the performance, an optimized way to do that. with respect to iconv, am... (3 Replies)
Discussion started by: matrixmadhan
3 Replies

10. UNIX for Advanced & Expert Users

iconv -l and ANSEL character set

I am forced to use the ANSEL character set for some GEDCOM documents but must convert them to a more modern set for another app which doesn't recognize ANSEL. I am unable to locate an ISO code for ANSEL in a search of the web. Would someone plese identify the ANSEL character set from the list given... (4 Replies)
Discussion started by: Whiterock
4 Replies
Login or Register to Ask a Question