Iconv UTF8 в KOI8 Invalid or incomplete multibyte or wide character

В iconv не работает //IGNORE для KOI8, почему не знаю, в код iconv не полез. Быстрое решение – использовать //TRANSLIT

#include <stdio.h>
#include <iconv.h>

int main(){
 char str[] = "\xC2\xAB\x0A"; // "«"
 size_t ret;
 char *bufptr = str;
 size_t len = sizeof(str), newlen = sizeof(str);
 iconv_t     to_cp1251       = (iconv_t) -1;
 iconv_t     to_koi8         = (iconv_t) -1;

 to_cp1251 = iconv_open("CP1251//IGNORE", "UTF8");
 if (to_cp1251 == (iconv_t) -1){
 return 1;

// Это не работает
 to_koi8 = iconv_open("KOI8R//IGNORE", "UTF8");
// А это работает
//    to_koi8 = iconv_open("KOI8R//TRANSLIT", "UTF8");
 if (to_koi8 == (iconv_t) -1){
 return 1;

 if( (ret=iconv(to_koi8, &bufptr, &len, &bufptr, &newlen)) == (size_t) -1 ){
 printf("iconv fail ret=%ld newlen=%ld len=%ld\n", ret, newlen, len);
 printf("Done: %s\n", bufptr);

 return 0;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s