괴로움을 거치지 않고 정복한 승리는 영광이 아니다. - Bonaparte Napoleon I
1 소개 #
- 홈페이지 :
http://www.gnu.org/software/libiconv/
- 각나라의 코드페이지로 문자열을 변환해주는 라이브러리입니다. 예를 들면, 완성형->조합형, 조합형->UTF-8과 같은 것이죠.
3.1 iconv_open() #
#include <iconv.h> iconv_t iconv_open (const char* tocode, const char* fromcode);
3.1.1 설명 #
iconv_open 함수는 문자 인코딩 fromcode에서 문자 인코딩 tocode 방식으로 문자열을 변환하기위한 변환기 핸들을 생성하는 역할을 합니다. fromcode와 tocode에 지정가능한 값과 두 값의 조합은 시스템마다 다릅니다. libiconv 라이브러리에서는 다음과 같은 인코딩이 지원되며 모두 상호조합이 가능합니다.
--enable-extra-encodings 옵션을 주어 빌드했을 경우 다음과 같은 추가된(잘쓰이지 않는) 인코딩 방식을 사용할 수 있습니다.
| 한국어 | EUC-KR, CP949, ISO-2022-KR, JOHAB |
| 유럽 | ASCII, ISO-8859-{1,2,3,4,5,7,9,10,13,14,15,16}, KOI8-R, KOI8-U, KOI8-RU, CP{1250,1251,1252,1253,1254,1257}, CP{850,866}, Mac{Roman,CentralEurope,Iceland,Croatian,Romania}, Mac{Cyrillic,Ukraine,Greek,Turkish}, Macintosh |
| 셈족 계통 | ISO-8859-{6,8}, CP{1255,1256}, CP862, Mac{Hebrew,Arabic} |
| 일본어 | EUC-JP, SHIFT_JIS, CP932, ISO-2022-JP, ISO-2022-JP-2, ISO-2022-JP-1 |
| 중국어 | EUC-CN, HZ, GBK, GB18030, EUC-TW, BIG5, CP950, BIG5-HKSCS, ISO-2022-CN, ISO-2022-CN-EXT |
| 아르메니아 | ARMSCII-8 |
| 그루지아 | Georgian-Academy, Georgian-PS |
| 타지키스탄 | KOI8-T |
| 타이어 | TIS-620, CP874, MacThai |
| 라오스 | MuleLao-1, CP1133 |
| 베트남 | VISCII, TCVN, CP1258 |
| 특정 플렛폼 전용 | HP-ROMAN8, NEXTSTEP |
| 유니코드 | UTF-8, UCS-2, UCS-2BE, UCS-2LE, UCS-4, UCS-4BE, UCS-4LE, UTF-16, UTF-16BE, UTF-16LE, UTF-32, UTF-32BE, UTF-32LE, UTF-7, C99, JAVA |
| uint16_t 또는 uint32_t에 기초한 유니코드(시스템 엔디안과 정렬방식에 의존적임) | UCS-2-INTERNAL, UCS-4-INTERNAL |
| char 또는 wchar_t에 기초한 로케일 의존적(시스템 엔디안과 정렬방식에 의존적임, LC_CTYPE 로케일 facet과 운영체계 설정에 따름) | char, wchar_t |
--enable-extra-encodings 옵션을 주어 빌드했을 경우 다음과 같은 추가된(잘쓰이지 않는) 인코딩 방식을 사용할 수 있습니다.
| 유럽 | CP{437,737,775,852,853,855,857,858,860,861,863,865,869,1125} |
| 셈계통 | CP864 |
| 일본어 | EUC-JISX0213, Shift_JISX0213, ISO-2022-JP-3 |
| 투르크멘 언어 | TDS565 |
| 특정 플렛폼 전용 | RISCOS-LATIN1 |
- 인코딩 명을 ""와 같이 공백으로 지정하면 이는 "char"로 지정한 것과 같이 처리됩니다 : 이는 로케일 의존적인 문자 인코딩 방식을 사용한다는 뜻이 됩니다.
- tocode에 "//TRANSLIT"을 덧붙여 지정하면 고쳐쓰기 기능이 활성화됩니다. 이는 특정 문자가 결과 문자집합에 존재하지 않을경우 비슷하게 보이는 글자중 하나로 어림잡아 대체한다는 의미입니다.
- tocode에 "//IGNORE"를 덧붙여 지정하면, 결과 문자집합에 존재하지 않는 문자들은 아무런 경고없이 건너뛰어 처리됩니다.
- 반환되는 변환기 핸들은 iconv()함수를 사용하여 몇번이고 실행이 가능합니다. 모든 처리가 끝나면 iconv_close()함수로 이 핸들을 닫아주면 됩니다.
- 변환기 핸들은 변환 상태를 담고 있습니다. iconv_open() 함수를 사용하여 생성된 직후에는, 상태는 초기 상태로 지정됩니다. iconv()함수를 사용하면 핸들의 변환상태를 수정하게 됩니다. (이는 다중 쓰레드 환경에서 동시에 하나의 핸들을 공유해서 사용할 수 없다는 것을 의미합니다) 상태를 다시 초기상태로 되돌리려면, iconv()에 inbuf 매개변수에 NULL을 지정하여 실행하세요.
3.2 iconv() #
#include <iconv.h> size_t iconv (iconv_t cd, const char* * inbuf, size_t * inbytesleft, char* * outbuf, size_t * outbytesleft);
3.2.1 설명 #
- 매개변수 cd는 iconv_open()함수를 사용하여 생성된 변환기 핸들이어야만 합니다.
- 기본적인 사용법은 inbuf 및 *inbuf 모두 NULL이 아닐 경우입니다. In this case, the iconv function converts the multibyte sequence starting at *inbuf to a multibyte sequence starting at *outbuf. At most *inbytesleft bytes, starting at *inbuf, will be read. At most *outbytesleft bytes, starting at *outbuf, will be written.
- iconv() 함수는 한번에 하나의 다중바이트 문자 하나를 변환합니다. 그리고 각각의 문자변환때마다 변환된 입력문자 바이트수만큼 *inbuf를 증가시키고 *inbytesleft를 감소시키게 되며, 변환된 출력문자 바이트수만큼 *outbuf을 증가시키고 *outbytesleft를 감소시킵니다. 그런다음 cd 변환기 핸들내의 변환 상태를 갱신합니다. 변환은 다음과 같은 4가지 경우에 정지하게 됩니다.
- An invalid multibyte sequence is encountered in the input. In this case it sets errno to EILSEQ and returns (size_t)(-1). *inbuf is left pointing to the beginning of the invalid multibyte sequence.
- The input byte sequence has been entirely converted, i.e. *inbytesleft has gone down to 0. In this case iconv returns the number of non-reversible conversions performed during this call.
- An incomplete multibyte sequence is encountered in the input, and the input byte sequence terminates after it. In this case it sets errno to EINVAL and returns (size_t)(-1). *inbuf is left pointing to the beginning of the incomplete multibyte sequence.
- The output buffer has no more room for the next converted character. In this case it sets errno to E2BIG and returns (size_t)(-1).
- A different case is when inbuf is NULL or *inbuf is NULL, but outbuf is not NULL and *outbuf is not NULL. In this case, the iconv function attempts to set cd's conversion state 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, it sets errno to E2BIG and returns (size_t)(-1). Otherwise it increments *outbuf and decrements *outbytesleft by the number of bytes written.
- inbuf가 NULL 또는 *inbuf가 NULL이며, outbuf가 NULL 또는 *outbuf가 NULL인 경우에는, iconv() 함수는 변환기 핸들 cd내의 변환 상태를 초기 상태로 변경합니다.
3.2.2 반환값 #
- iconv() function returns the number of characters converted in a non-reversible way during this call; reversible conversions are not counted.
- 오류가 발생하면 errno에 오류코드를 넣고 (iconv_t)(-1)를 반환합니다.








