1iconv(3) Library Functions Manual iconv(3)
2
3
4
6 iconv - perform character set conversion
7
9 Standard C library (libc, -lc)
10
12 #include <iconv.h>
13
14 size_t iconv(iconv_t cd,
15 char **restrict inbuf, size_t *restrict inbytesleft,
16 char **restrict outbuf, size_t *restrict outbytesleft);
17
19 The iconv() function converts a sequence of characters in one character
20 encoding to a sequence of characters in another character encoding.
21 The cd argument is a conversion descriptor, previously created by a
22 call to iconv_open(3); the conversion descriptor defines the character
23 encodings that iconv() uses for the conversion. The inbuf argument is
24 the address of a variable that points to the first character of the in‐
25 put sequence; inbytesleft indicates the number of bytes in that buffer.
26 The outbuf argument is the address of a variable that points to the
27 first byte available in the output buffer; outbytesleft indicates the
28 number of bytes available in the output buffer.
29
30 The main case is when inbuf is not NULL and *inbuf is not NULL. In
31 this case, the iconv() function converts the multibyte sequence start‐
32 ing at *inbuf to a multibyte sequence starting at *outbuf. At most
33 *inbytesleft bytes, starting at *inbuf, will be read. At most *out‐
34 bytesleft bytes, starting at *outbuf, will be written.
35
36 The iconv() function converts one multibyte character at a time, and
37 for each character conversion it increments *inbuf and decrements *in‐
38 bytesleft by the number of converted input bytes, it increments *outbuf
39 and decrements *outbytesleft by the number of converted output bytes,
40 and it updates the conversion state contained in cd. If the character
41 encoding of the input is stateful, the iconv() function can also con‐
42 vert a sequence of input bytes to an update to the conversion state
43 without producing any output bytes; such input is called a shift se‐
44 quence. The conversion can stop for four reasons:
45
46 • An invalid multibyte sequence is encountered in the input. In this
47 case, it sets errno to EILSEQ and returns (size_t) -1. *inbuf is
48 left pointing to the beginning of the invalid multibyte sequence.
49
50 • The input byte sequence has been entirely converted, that is, *in‐
51 bytesleft has gone down to 0. In this case, iconv() returns the
52 number of nonreversible conversions performed during this call.
53
54 • An incomplete multibyte sequence is encountered in the input, and
55 the input byte sequence terminates after it. In this case, it sets
56 errno to EINVAL and returns (size_t) -1. *inbuf is left pointing to
57 the beginning of the incomplete multibyte sequence.
58
59 • The output buffer has no more room for the next converted character.
60 In this case, it sets errno to E2BIG and returns (size_t) -1.
61
62 A different case is when inbuf is NULL or *inbuf is NULL, but outbuf is
63 not NULL and *outbuf is not NULL. In this case, the iconv() function
64 attempts to set cd's conversion state to the initial state and store a
65 corresponding shift sequence at *outbuf. At most *outbytesleft bytes,
66 starting at *outbuf, will be written. If the output buffer has no more
67 room for this reset sequence, it sets errno to E2BIG and returns
68 (size_t) -1. Otherwise, it increments *outbuf and decrements *out‐
69 bytesleft by the number of bytes written.
70
71 A third case is when inbuf is NULL or *inbuf is NULL, and outbuf is
72 NULL or *outbuf is NULL. In this case, the iconv() function sets cd's
73 conversion state to the initial state.
74
76 The iconv() function returns the number of characters converted in a
77 nonreversible way during this call; reversible conversions are not
78 counted. In case of error, iconv() returns (size_t) -1 and sets errno
79 to indicate the error.
80
82 The following errors can occur, among others:
83
84 E2BIG There is not sufficient room at *outbuf.
85
86 EILSEQ An invalid multibyte sequence has been encountered in the input.
87
88 EINVAL An incomplete multibyte sequence has been encountered in the in‐
89 put.
90
92 For an explanation of the terms used in this section, see at‐
93 tributes(7).
94
95 ┌────────────────────────────────────┬───────────────┬─────────────────┐
96 │Interface │ Attribute │ Value │
97 ├────────────────────────────────────┼───────────────┼─────────────────┤
98 │iconv() │ Thread safety │ MT-Safe race:cd │
99 └────────────────────────────────────┴───────────────┴─────────────────┘
100
101 The iconv() function is MT-Safe, as long as callers arrange for mutual
102 exclusion on the cd argument.
103
105 POSIX.1-2008.
106
108 glibc 2.1. POSIX.1-2001.
109
111 In each series of calls to iconv(), the last should be one with inbuf
112 or *inbuf equal to NULL, in order to flush out any partially converted
113 input.
114
115 Although inbuf and outbuf are typed as char **, this does not mean that
116 the objects they point can be interpreted as C strings or as arrays of
117 characters: the interpretation of character byte sequences is handled
118 internally by the conversion functions. In some encodings, a zero byte
119 may be a valid part of a multibyte character.
120
121 The caller of iconv() must ensure that the pointers passed to the func‐
122 tion are suitable for accessing characters in the appropriate character
123 set. This includes ensuring correct alignment on platforms that have
124 tight restrictions on alignment.
125
127 iconv_close(3), iconv_open(3), iconvconfig(8)
128
129
130
131Linux man-pages 6.04 2023-03-30 iconv(3)