ICONV(3P)
π ICONV(3P) β POSIX Programmer's Manual π
ok so bestie, before we even get into it β this one is from the POSIX Programmer's Manual. Quick lil heads up:
πΈ PROLOG πΈ
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface may not be implemented on Linux at all. π
β¨ NAME β¨
iconv β codeset conversion function
the way this is literally the glow-up function for character encodings, iykyk π
π 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 π¦
Okay so here's the vibe: iconv() converts a sequence of characters from one codeset β the one sitting in the array pointed to by inbuf β into a sequence of corresponding characters in another codeset, in the array pointed to by outbuf. The codesets in question? Those are the ones you picked in the iconv_open() call that handed you back the conversion descriptor, cd. π
So inbuf points to a variable that points to the first character in the input buffer, and inbytesleft tells you how many bytes there are from there to the end of the buffer that still need converting. Meanwhile outbuf points to a variable that points to the first available byte in the output buffer, and outbytesleft is how many available bytes you've got from there to the end of that buffer.
For state-dependent encodings, the conversion descriptor cd gets placed into its initial shift state by a call where inbuf is a null pointer, or where inbuf points to a null pointer. When you call iconv() like that, and if outbuf is not a null pointer or a pointer to a null pointer, and outbytesleft points to a positive value, then iconv() shall place into the output buffer the byte sequence needed to change the output buffer to its initial shift state. But β and this is the important part β if the output buffer isn't big enough to hold that entire reset sequence, iconv() shall fail and set errno to [E2BIG]. π Subsequent calls where inbuf is anything other than a null pointer or a pointer to a null pointer make the conversion happen from the current state of the conversion descriptor.
Now, the messy parts, and there are a few:
- If a sequence of input bytes doesn't form a valid character in the specified codeset, conversion shall stop after the previous successfully converted character.
- If the input buffer ends with an incomplete character or shift sequence, conversion shall stop after the previous successfully converted bytes.
- If the output buffer isn't large enough to hold the entire converted input, conversion shall stop just before the input bytes that would cause the output buffer to overflow.
And after all that, the bookkeeping goes like this:
- The variable pointed to by
inbufshall be updated to point to the byte following the last byte successfully used in the conversion. - The value pointed to by
inbytesleftshall be decremented to reflect the number of bytes still not converted in the input buffer. - The variable pointed to by
outbufshall be updated to point to the byte following the last byte of converted output data. - The value pointed to by
outbytesleftshall be decremented to reflect the number of bytes still available in the output buffer. - For state-dependent encodings, the conversion descriptor shall be updated to reflect the shift state in effect at the end of the last successfully converted byte sequence.
And one more thing: if iconv() encounters a character in the input buffer that is valid, but for which an identical character does not exist in the target codeset, iconv() shall perform an implementation-defined conversion on that character. (yes bestie, implementation-defined, meaning don't come crying to POSIX about it π)
π RETURN VALUE π
iconv() shall update the variables pointed to by the arguments to reflect how far the conversion got, and it returns the number of non-identical conversions performed.
- If the entire string in the input buffer is converted, the value pointed to by
inbytesleftshall be0. - If the input conversion is stopped for any of the conditions mentioned above, the value pointed to by
inbytesleftshall be non-zero anderrnoshall be set to indicate the condition. - If an error occurs,
iconv()shall return(size_t)-1and seterrnoto indicate the error.
π₯ ERRORS π₯
The iconv() function shall fail if:
EILSEQβ Input conversion stopped due to an input byte that does not belong to the input codeset.E2BIGβ Input conversion stopped due to lack of space in the output buffer.EINVALβ Input conversion stopped due to an incomplete character or shift sequence at the end of the input buffer.
The iconv() function may fail if:
EBADFβ Thecdargument is not a valid open conversion descriptor.
The following sections are informative. (so like, chill, these are bonus content, not the rules-rules π)
π EXAMPLES π
None. not a single one. iconic behavior honestly π
π«Ά APPLICATION USAGE π«Ά
ok so this part is lowkey the most important vibe check of the whole page:
The inbuf argument indirectly points to the memory area containing the conversion input data. The outbuf argument indirectly points to the memory area that's gonna hold the result of the conversion. And here's the thing β the objects indirectly pointed to by inbuf and outbuf are not restricted to containing data that's directly representable in the ISO C standard language char data type. The type of inbuf and outbuf, char **, does not imply that the objects pointed to are interpreted as null-terminated C strings or arrays of characters. π¨
Any interpretation of a byte sequence that represents a character in a given character set encoding scheme happens internally within the codeset converters. For example, the area pointed to indirectly by inbuf and/or outbuf can contain all zero octets that are not interpreted as string terminators but as coded character data according to the respective codeset encoding scheme. The type of the data (char, short, long, and so on) read or stored in the objects is not specified, but it may be inferred for both the input and output data by the converters determined by the fromcode and tocode arguments of iconv_open().
And regardless of whatever data type the converter inferred, the size of the remaining space in both the input and output objects β the intbytesleft and outbytesleft arguments β is always measured in bytes. always. no exceptions. π
For implementations that support converting state-dependent encodings, the conversion descriptor must be able to accurately reflect the shift-state in effect at the end of the last successful conversion. It is not required that the conversion descriptor itself be updated β because that would require it to be a pointer type. So implementations are free to implement the descriptor as a handle (something other than a pointer type) through which the conversion information can be accessed and updated. cute and flexible of them β¨
π RATIONALE π
None. (the reasoning is simply that it slays, periodt)
β FUTURE DIRECTIONS β
None.
πΈ SEE ALSO πΈ
iconv_open()iconv_close()mbsrtowcs()- The Base Definitions volume of POSIX.1β2017,
<iconv.h>
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole thing bestie π«Ά this page came to you from IEEE/The Open Group, 2017, and the original page header was ICONV(3P) β go forth and convert some codesets, you've got this πβ¨π
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- 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-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms