iconv

Section: C Library Functions (3)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

iconv - 文字セット変換を行う  

LIBRARY

標準 C ライブラリ (libc,~-lc)  

書式

#include <iconv.h>

size_t iconv(iconv_t cd,
             char **restrict inbuf, size_t *restrict inbytesleft,
             char **restrict outbuf, size_t *restrict outbytesleft);
 

説明

iconv() 関数は、ある文字エンコーディングの文字シーケンス列を別の文字 エンコーディングの文字シーケンスに変換する。cd 引数は変換ディスク リプタ (conversion descriptor) であり、以前は iconv_open(3) を呼び出 すことで生成されていた。変換ディスクリプターは iconv() が変換に使用す る文字エンコーディングを定義するものである。 inbuf 引数は入力シー ケンスの先頭バイトを指す変数のアドレスであり、inbytesleft は入力シー ケンスのバッファーのバイト数を示す。outbuf 引数は出力バッファーで利用 できる先頭バイトを指す変数のアドレスであり、 outbytesleft は出力 バッファーのバイト数を示す。

主に使われるのは、 「inbuf が NULL でなく、かつ *inbuf が NULL でない」 という場合である。 この場合、 iconv() 関数は、 *inbuf で始まるマルチバイト文字列を *outbuf で始まるマルチバイト文字列に変換する。 *inbuf を先頭として最大 *inbytesleft バイトが読み込まれ、 *outbuf を先頭として最大 *outbytesleft バイトが書き出される。

The iconv() function converts one multibyte character at a time, and for each character conversion it increments *inbuf and decrements *inbytesleft by the number of converted input bytes, it increments *outbuf and decrements *outbytesleft by the number of converted output bytes, and it updates the conversion state contained in cd. If the character encoding of the input is stateful, the iconv() function can also convert a sequence of input bytes to an update to the conversion state without producing any output bytes; such input is called a shift~sequence. The conversion can stop for five reasons:

•
入力に無効なマルチバイト文字列があった場合。この場合、 関数は errno を EILSEQ に設定し、 (size_t) -1 を返す。 *inbuf は、無効なマルチバイト文字列の先頭を指したままになる。
•
A multibyte sequence is encountered that is valid but that cannot be translated to the character encoding of the output. This condition depends on the implementation and on the conversion descriptor. In the GNU C library and GNU libiconv, if cd was created without the suffix //TRANSLIT or //IGNORE, the conversion is strict: lossy conversions produce this condition. If the suffix //TRANSLIT was specified, transliteration can avoid this condition in some cases. In the musl C library, this condition cannot occur because a conversion to '*' is used as a fallback. In the FreeBSD, NetBSD, and Solaris implementations of iconv(), this condition cannot occur either, because a conversion to '?' is used as a fallback. When this condition is met, iconv() sets errno to EILSEQ and returns (size_t) -1. *inbuf is left pointing to the beginning of the unconvertible multibyte sequence.
•
入力バイト文字列が完全に変換され、*inbytesleft が 0 になった場合。 この場合、 iconv() は呼出しの間に非可逆変換が行われた回数を返す。
•
入力に不完全なマルチバイト文字列があり、入力バイト文字列がその後で終了 している場合。この場合、関数は、errno を EINVAL に設定し、 (size_t) -1 を返す。 *inbuf は、不完全なマルチバイト文字列の先頭 を指したままにされる。
•
出力バッファーに次の変換された文字列のための空きがない場合。 この場合、 errno が E2BIG に設定され、 (size_t) -1 が返される。

別のケースとしては、 「inbuf が NULL、または *inbuf が NULL である。 しかし、outbuf が NULL でなく、かつ *outbuf が NULL でない」 という場合がある。 この場合、 iconv() 関数は、cd の変換状態を初期状態にして、 対応するシフト文字列を *outbuf に保存しようとする。 最大 *outbytesleft バイトが、*outbuf を始めとして書き出される。 このリセットされた文字列に対して、出力バッファーに空きがない場合、 この関数は errno を E2BIG に設定し、 (size_t) -1 を返す。 それ以外の場合、この関数は、書き込まれたバイトの数だけ *outbuf を増加させ、*outbytesleft を減少させる。

3 番目のケースしては、 「inbuf が NULL、または *inbuf が NULL である。 かつ、outbuf が NULL、または *outbuf が NULL である」 という場合がある。 この場合、 iconv() 関数は、cd の変換状態を初期状態にする。  

返り値

The iconv() function returns the number of characters converted in a nonreversible way during this call; reversible conversions are not counted. In case of error, iconv() returns (size_t) -1 and sets errno to indicate the error.  

エラー

他のいろいろなエラーのうちから、以下のエラーが起こりうる。
E2BIG
*outbuf に十分な空きがない。
EILSEQ
入力に無効なマルチバイト文字列があった。
EINVAL
入力に不完全なマルチバイト文字列があった。
 

属性

この節で使用されている用語の説明については、 attributes(7) を参照。
インターフェース属性値
iconv() Thread safetyMT-Safe race:cd

The iconv() function is MT-Safe, as long as callers arrange for mutual exclusion on the cd argument.  

標準規格

POSIX.1-2008.  

歴史

glibc 2.1. POSIX.1-2001.  

注意

In each series of calls to iconv(), the last should be one with inbuf or *inbuf equal to NULL, in order to flush out any partially converted input.

inbuf と outbuf は char ** 型だが、これらの変数が指す オブジェクトが C の文字列、つまり文字の配列として解釈されることを意味 するわけではない。文字バイトシーケンスの解釈は変換関数の内部で行われる。 エンコーディングによっては、バイト 0 もマルチバイト文字の有効な 構成要素の場合がある。

iconv() の呼び出し元は、 iconv() に渡すポインターが、 必要な文字集合の文字にアクセスするのに適したものとなっていることを 保証しなければならない。これには、アライメントに関して厳しい制限が あるプラットフォームにおいて正しいアライメントになっていることを 保証するといったことも含まれる。  

関連項目

iconv_close(3), iconv_open(3), iconvconfig(8)


 

Index

名前
LIBRARY
書式
説明
返り値
エラー
属性
標準規格
歴史
注意
関連項目

This document was created by man2html, using the manual pages.