0

I am working with a vendor that is running on Windows and expects text in UTF16. I'm running on Linux and it's UTF8. I'm trying to use iconv to convert from UTF8 to UTF16 so I can send over a socket connection. I found an article here stackOverflow, and tried to follow that code but nothing is returned from my function. Has anyone had any experience converting from UTF-8 to UTF-16? thanks.

char * Convert( char *from_charset, char *to_charset, char *input )
{
size_t input_size, output_size, bytes_converted;
char * output;
char * tmp;
iconv_t cd;

cd = iconv_open( to_charset, from_charset);
if ( cd == (iconv_t) -1 )
{
    //Something went wrong with iconv_open
    if (errno == EINVAL)
    {
        char * buffer;
        sprintf(buffer, "Conversion from %s to %s not available", from_charset, to_charset);
        cout << buffer << endl;
    }
    return NULL;
}

input_size = strlen(input);
output_size = 2 * input_size;
output = (char*) malloc(output_size+1);

bytes_converted = iconv(cd, &input, &input_size, &output, &output_size);
cout << "Bytes converted: " << bytes_converted << endl;
if ( iconv_close (cd) != 0)
    cout<< "Error closing iconv_error" << endl;

return output;

}

Community
  • 1
  • 1
tankmr
  • 1
  • 1
  • 2

1 Answers1

0

the iconv function takes the address of the pointers to strings and as it s converting it points to the next element of the char array. when it finish the output actually points to the last element of the array , it can be null or something else. you need to save the starting address of the output string in a temp location . before feeding output to iconv do the following char * tempoutput = output ;

and then after iconv call return tempoutput;

and dont forget to free(output) and the returned string is also presistent you need to free it after using convert.

Nadim Farhat
  • 446
  • 3
  • 11