Name

iconv - performs character set conversion

Library

libc.lib

Synopsis

  #include <iconv.h>
  size_t iconv (iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
Sh RETURN VALUES If the entire input string is converted the iconv function returns 0. If the conversion is stopped due to some reasons the inputbytesleft will be non-zero. In these case of errors,it sets errno and returns (size_t)(-1).

Detailed description

cd is a conversion descriptor created using the function iconv_open.

The iconv function sets errno to EBADF ,if the conversion descriptor is a bad descriptor.

There are three cases. They are as follows:

The first case is when inbuf is not NULL and *inbuf is not NULL. In this case the iconv function converts the multibyte character string starting at *inbuf to a multibyte character sequence starting at *outbuf from source character set to destination character set.

The iconv function converts one multibyte character at a time, and for each character conversion it increments *inbuf by the number of converted input bytes and decrements *inbytesleft by the number of converted input bytes, it increments *outbuf by the number of converted output bytes and decrements *outbytesleft by the number of converted output bytes.

The conversion can stop for four reasons:

1.An invalid multibyte character sequence is encountered in the input. In this case it sets errno to EILSEQ and returns (size_t)(-1).*inbuf is left pointing to the beginning of the invalid multibyte character sequence.

2.The input multibyte sequence is fully converted, i.e when *inbytesleft reach 0. In this case iconv returns 0.

3.An incomplete multibyte sequence is encountered in the input and the input sequence ends there. In this case it sets errno to EINVAL and returns (size_t)(-1). *inbuf is made to point to the beginning of the incomplete multibyte sequence.

4. The *outbytesleft reached 0 i.e output buffer has no enough space for the next converted character. In this case it sets errno to E2BIG and returns (size_t)(-1).

The second case is when inbuf is NULL or *inbuf is NULL. In this case if outbuf is not NULL and *outbuf is not NULL the iconv returns 0. If *outbytesleft reached 0 i.e output buffer has no enough space for the next converted character it sets errno to E2BIG and returns (size_t)(-1).

A third case is when inbuf is NULL or *inbuf is NULL, and outbuf is NULL or *outbuf is NULL. In this case, the iconv function returns 0.


Examples

#include<stdio.h>
#include<iconv.h>
int main()
{
       iconv_t cd = NULL;
       char utf8[12] = "abcd";
       char iso[12];
       const char *inbuf;
       char *outbuf;
       size_t inbytes = 4;
       size_t outbytes = 4;
       inbuf = (const char*) utf8;
       outbuf = (char *) iso;
       //Allocate descriptor for charset conversion
       cd = iconv_open("UTF-8", "ISO-8859-1");
       if(cd == (iconv_t) -1)
       {
               printf("Unbale to create a conversion descriptor0);
       }
       else
       {
               printf("Conversion descriptor is created successfully0);
       }
       printf("before conversion inbytes = %d outbytes = %d0, inbytes, outbytes);
       iconv (cd, &inbuf, &inbytes, &outbuf, &outbytes);
       printf("After conversion inbytes = %d outbytes = %d0, inbytes, outbytes);
       iconv_close(cd);
       return 0;
}

         

Output

Conversion descriptor is created successfully
before conversion inbytes = 4 outbytes = 4
before conversion inbytes = 0 outbytes = 0

         

         

Errors

The following errors can occur, among others:
E2BIG:There is not sufficient room in the output buffer i.e *outbuf is 0.
EILSEQ: An invalid multibyte sequence has been encountered in the input which does not belong to the input character set.
EINVAL: An incomplete multibyte sequence has been encountered in the input.
EBADF: cd is not a valid conversion descriptor

See also

iconv_open, iconv_close

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top