iconv(3)
๐ซ iconv(3) โ the glow-up where your bytes change outfits ๐ซ
okay bestie, pull up a chair ๐ซถ this is iconv(3) from the Library Functions Manual, living in the Standard C library (libc, -lc). She converts your characters from one encoding to another, and honestly? iconic behaviour. no cap.
๐ธ NAME ๐ธ
iconv โ perform character set conversion โจ
๐ LIBRARY ๐
Standard C library (libc, -lc) ๐
๐ SYNOPSIS ๐
#include <iconv.h>
size_t iconv(iconv_t cd,
char **restrict inbuf, size_t *restrict inbytesleft,
char **restrict outbuf, size_t *restrict outbytesleft);
๐ฆ DESCRIPTION ๐ฆ
so like, the iconv() function converts a sequence of characters in one character encoding into a sequence of characters in a different character encoding. she's a translator, she's multilingual, she's giving main character energy ๐
cdโ the conversion descriptor. this is something you already made by callingiconv_open(3)earlier. it's the one that defines which two encodingsiconv()is using for the conversion. nocd, no vibes.inbufโ the address of a variable that points to the first character of the input sequence. yes, address-of-a-pointer, we love a double pointer moment ๐inbytesleftโ how many bytes are chillin' in that input buffer.outbufโ the address of a variable pointing to the first byte available in the output buffer.outbytesleftโ how many bytes are available in the output buffer.
โจ the main case (the one you'll actually use) โจ
this is when inbuf is not NULL and *inbuf is not NULL. in this case iconv() converts the multibyte sequence starting at *inbuf into a multibyte sequence starting at *outbuf.
- at most
*inbytesleftbytes, starting at*inbuf, will be read ๐ - at most
*outbytesleftbytes, starting at*outbuf, will be written โ๏ธ
she converts one multibyte character at a time, and for each character conversion she does the lil bookkeeping dance:
- increments
*inbufand decrements*inbytesleftby the number of converted input bytes - increments
*outbufand decrements*outbytesleftby the number of converted output bytes - updates the conversion state contained inside
cd
and here's a fun one: if the input's character encoding is stateful, iconv() can also convert a sequence of input bytes into an update to the conversion state without producing any output bytes at all. that kind of input is called a shift sequence. sneaky. love that for her.
the conversion can stop for five reasons, and bestie you gotta know all five:
- ๐ญ an invalid multibyte sequence shows up in the input. she sets
errnotoEILSEQand returns(size_t) -1.*inbufis left pointing at the beginning of the invalid multibyte sequence. - ๐ญ a multibyte sequence shows up that's valid but can't be translated into the output's character encoding. this one depends on the implementation and on the conversion descriptor. in the GNU C library and GNU libiconv, if
cdwas created without the suffix//TRANSLITor//IGNORE, the conversion is strict: lossy conversions produce this condition. if the suffix//TRANSLITwas specified, transliteration can avoid this condition in some cases. in the musl C library, this condition can't occur at all, because a conversion to'*'is used as a fallback. in the FreeBSD, NetBSD, and Solaris implementations oficonv(), this condition can't occur either, because a conversion to'?'is used as a fallback. when this condition is met,iconv()setserrnotoEILSEQand returns(size_t) -1.*inbufis left pointing at the beginning of the unconvertible multibyte sequence. - โ
the input byte sequence has been entirely converted, i.e.
*inbyteslefthas gone down to0. in this caseiconv()returns the number of nonreversible conversions performed during this call. slay. - ๐ญ an incomplete multibyte sequence shows up in the input, and the input byte sequence terminates right after it. she sets
errnotoEINVALand returns(size_t) -1.*inbufis left pointing at the beginning of the incomplete multibyte sequence. - ๐ญ the output buffer has no more room for the next converted character. she sets
errnotoE2BIGand returns(size_t) -1.
๐ the second case (the reset/flush moment) ๐
different scenario: inbuf is NULL or *inbuf is NULL, but outbuf is not NULL and *outbuf is not NULL.
here, iconv() tries to set cd's conversion state back to the initial state and store a corresponding shift sequence at *outbuf. at most *outbytesleft bytes, starting at *outbuf, will be written.
if the output buffer has no more room for this reset sequence, she sets errno to E2BIG and returns (size_t) -1. otherwise she increments *outbuf and decrements *outbytesleft by the number of bytes written. โจ clean slate energy โจ
๐ง the third case (the quiet one) ๐ง
third case: inbuf is NULL or *inbuf is NULL, and outbuf is NULL or *outbuf is NULL.
in this case iconv() just sets cd's conversion state to the initial state. that's it. no output, no drama, just vibes.
๐ฅ RETURN VALUE ๐ฅ
iconv() returns the number of characters converted in a nonreversible way during this call โ reversible conversions don't get counted, they're just out here living rent-free ๐
in case of error, iconv() returns (size_t) -1 and sets errno to indicate the error.
๐ ERRORS ๐
the following errors can occur, among others (so this is not an exhaustive list, okay?):
E2BIGโ there isn't sufficient room at*outbuf. your output buffer said "no more room bestie" ๐ซEILSEQโ an invalid multibyte sequence has been encountered in the input.EINVALโ an incomplete multibyte sequence has been encountered in the input.
๐ ATTRIBUTES ๐
for an explanation of the terms used in this section, see attributes(7).
- Interface:
iconv()- Attribute: Thread safety
- Value:
MT-Safe race:cd
the iconv() function is MT-Safe, as long as callers arrange for mutual exclusion on the cd argument. so like, thread-safe but only if y'all take turns with cd, okay?? no fighting over the descriptor ๐ค
๐ STANDARDS ๐
POSIX.1-2008.
๐ฐ๏ธ HISTORY ๐ฐ๏ธ
glibc 2.1. POSIX.1-2001.
๐ NOTES ๐
- in each series of calls to
iconv(), the last one should be a call withinbufor*inbufequal toNULL, so you can flush out any partially converted input. don't skip the flush, bestie, it's not optional in spirit ๐ฟ - even though
inbufandoutbufare typed aschar **, that does not mean the objects they point to can be interpreted as C strings or as arrays of characters. the interpretation of character byte sequences is handled internally by the conversion functions. in some encodings, a zero byte may be a valid part of a multibyte character. so do NOT go treating them as cute lil strings, that's a trap ๐ - the caller of
iconv()must ensure that the pointers passed to the function are suitable for accessing characters in the appropriate character set. this includes ensuring correct alignment on platforms that have tight restrictions on alignment. alignment matters, periodt.
๐ซถ SEE ALSO ๐ซถ
iconv_close(3), iconv_open(3), iconvconfig(8)
๐ that's the whole page! go convert something and remember: flush your state, check yourerrno, and never assumechar **means "string" ๐
and that's a wrap on iconv(3), straight from Linux man-pages 6.20, dated 2026-02-08. mwah ๐
โจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: GPL-2.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms