1ICONV(3) Linux Programmer's Manual ICONV(3)
2
3
4
6 iconv - perform character set conversion
7
9 #include <iconv.h>
10
11 size_t iconv(iconv_t cd,
12 char **restrict inbuf, size_t *restrict inbytesleft,
13 char **restrict outbuf, size_t *restrict outbytesleft);
14
16 The iconv() function converts a sequence of characters in one character
17 encoding to a sequence of characters in another character encoding.
18 The cd argument is a conversion descriptor, previously created by a
19 call to iconv_open(3); the conversion descriptor defines the character
20 encodings that iconv() uses for the conversion. The inbuf argument is
21 the address of a variable that points to the first character of the in‐
22 put sequence; inbytesleft indicates the number of bytes in that buffer.
23 The outbuf argument is the address of a variable that points to the
24 first byte available in the output buffer; outbytesleft indicates the
25 number of bytes available in the output buffer.
26
27 The main case is when inbuf is not NULL and *inbuf is not NULL. In
28 this case, the iconv() function converts the multibyte sequence start‐
29 ing at *inbuf to a multibyte sequence starting at *outbuf. At most
30 *inbytesleft bytes, starting at *inbuf, will be read. At most *out‐
31 bytesleft bytes, starting at *outbuf, will be written.
32
33 The iconv() function converts one multibyte character at a time, and
34 for each character conversion it increments *inbuf and decrements *in‐
35 bytesleft by the number of converted input bytes, it increments *outbuf
36 and decrements *outbytesleft by the number of converted output bytes,
37 and it updates the conversion state contained in cd. If the character
38 encoding of the input is stateful, the iconv() function can also con‐
39 vert a sequence of input bytes to an update to the conversion state
40 without producing any output bytes; such input is called a shift se‐
41 quence. The conversion can stop for four reasons:
42
43 1. An invalid multibyte sequence is encountered in the input. In this
44 case, it sets errno to EILSEQ and returns (size_t) -1. *inbuf is
45 left pointing to the beginning of the invalid multibyte sequence.
46
47 2. The input byte sequence has been entirely converted, that is, *in‐
48 bytesleft has gone down to 0. In this case, iconv() returns the
49 number of nonreversible conversions performed during this call.
50
51 3. An incomplete multibyte sequence is encountered in the input, and
52 the input byte sequence terminates after it. In this case, it sets
53 errno to EINVAL and returns (size_t) -1. *inbuf is left pointing to
54 the beginning of the incomplete multibyte sequence.
55
56 4. The output buffer has no more room for the next converted character.
57 In this case, it sets errno to E2BIG and returns (size_t) -1.
58
59 A different case is when inbuf is NULL or *inbuf is NULL, but outbuf is
60 not NULL and *outbuf is not NULL. In this case, the iconv() function
61 attempts to set cd's conversion state to the initial state and store a
62 corresponding shift sequence at *outbuf. At most *outbytesleft bytes,
63 starting at *outbuf, will be written. If the output buffer has no more
64 room for this reset sequence, it sets errno to E2BIG and returns
65 (size_t) -1. Otherwise, it increments *outbuf and decrements *out‐
66 bytesleft by the number of bytes written.
67
68 A third case is when inbuf is NULL or *inbuf is NULL, and outbuf is
69 NULL or *outbuf is NULL. In this case, the iconv() function sets cd's
70 conversion state to the initial state.
71