0

I am using c++/XMS on AIX for consuming incoming messages from MQ. Rightnow I have a need for converting the message from ISO8859-1 to UTF-8. I do the following.

======================================================

void iso2utf8( char* text_iso, char* text_utf8, int nLen)
{
    cout << "Converting to UTF-8." << endl;
    iconv_t ic;
    ic = iconv_open("UTF-8", "ISO8859-1"); // iso->utf8
    cout << "Size of text_iso" << sizeof(text_iso) << endl;

    size_t il = nLen;
    size_t ol = nLen;

    cout << "Size of text_iso" << ol << endl;
    iconv(ic , &text_iso, &il, &text_utf8, &ol);
    iconv_close(ic);

    cout << "Message in UTF-8: " << text_utf8 << endl;

    return;
}

======================================================

After conversion, when I save the message into a file, I get back the ISO8859-1 message. Any tips on how to solve this. I use fstream to write to file.

JoshMc
  • 10,239
  • 2
  • 19
  • 38
  • You have a total lack of error checking in your code - all those iconv* calls can fail - you should be checking the result from each (error gives a result of -1 and sets `errno`) - that way you at least get a clue as to why your code is not working. – Paul R Apr 05 '17 at 07:07
  • It does work for me (both with built-in iconv and GNU-iconv), so I suggest you create a mcve http://stackoverflow.com/help/mcve – Lorinczy Zsigmond Apr 05 '17 at 10:00
  • Thanks for your comments. OK. I make appropriate changes and revert soon. – Madhu Cheluvaraju Apr 05 '17 at 11:24
  • Mind you, using 'nLen' for both input and output is bad idea: utf8 is usually longer than iso-8859-1, as 0x80..0xff codes become 2-byte sequences. – Zsigmond Lőrinczy Aug 06 '21 at 19:11

1 Answers1

0

You should write "ISO-8859-1" instead of "ISO8859-1"

Link to iconv man: https://www.gnu.org/software/libiconv/documentation/libiconv-1.13/iconv_open.3.html

Citation: The values permitted for fromcode and tocode and the supported combinations are system dependent. For the libiconv library, the following encodings are supported, in all combinations.

European languages

ASCII, ISO−8859−{1,2,3,4,5,7,9,10,13,14,15,16} ...