#include #include #include #include #include #include #include int main() { cout << hex; /* Unicode value of russian 'o' is 0x43e, the value in KOI8-R encoding is 207 (0xcf). */ { short val = 0x3e04; iconv_t cd = iconv_open("KOI8-R","UCS2"); if (cd==(iconv_t)-1) { cout << "can't open iconv_t for conversion from \"UCS2\" to \"KOI8-R\"\n"; return 1; }; char buf[10]; char* in =(char*)&val,*out = buf; size_t inlen = 2, outlen = 1; size_t done = iconv(cd,(const char**)&in,&inlen,&out,&outlen); if (inlen==0) { cout << "conversion from \"UCS2\" to \"KOI8-R\" OK " "with 0x" << val << " on input and 0x" << unsigned((unsigned char)(buf[0])) << " on output\n"; } else { cout << "conversion from \"UCS2\" to \"KOI8-R\" failed " "with 0x" << val << " on input\n"; }; cout << "iconv returned " << done << "\n"; } { short val = 0x043e;/*swapped bytes*/ iconv_t cd = iconv_open("KOI8-R","UCS2"); if (cd==(iconv_t)-1) { cout << "can't open cd1\n"; return 2; }; char buf[10]; char* in =(char*)&val,*out = buf; size_t inlen = 2, outlen = 1; size_t done = iconv(cd,(const char**)&in,&inlen,&out,&outlen); if (inlen==0) { cout << "conversion from \"UCS2\" to \"KOI8-R\" OK " "with 0x" << val << " on input and 0x" << unsigned((unsigned char)(buf[0])) << " on output\n"; } else { cout << "conversion from \"UCS2\" to \"KOI8-R\" failed " "with 0x" << val << " on input\n"; }; cout << "iconv returned " << done << "\n"; } { short val[1]; iconv_t cd = iconv_open("UCS2","KOI8-R"); if (cd==(iconv_t)-1) cout << "can't open can't open iconv_t for conversion from \"KOI8-R\" to \"UCS2\"\n"; char buf = 207 ; char* in = &buf,*out = (char*)val; size_t inlen = 1, outlen = 10; size_t done = iconv(cd,(const char**)&in,&inlen,&out,&outlen); if (inlen==0) { cout << "conversion from \"KOI8-R\" to \"UCS2\" OK " "with 0x" << unsigned((unsigned char)buf) << " on input and 0x" << val[0] << " on output\n"; } else { cout << "conversion from \"KOI8-R\" to \"UCS2\" failed " "with 0x" << val[0] << " on input\n"; }; cout << "iconv returned " << done << "\n"; } };