I am using a library which has a function that returns result strings encoded as UTF-16LE (I'm pretty sure) in a standard char *, as well as the number of bytes in the string. I would like to convert these strings to UTF-8. I tried the solution from this question: Convert UTF-16 to UTF-8 under Windows and Linux, in C which says to use iconv, however the result was that both input and output buffers wound up empty. What am I missing?
My input and output buffers are declared and initialized as follows:
char *resbuff=NULL;
char *outbuff=NULL;
int stringLen;
size_t outbytes=1024;
size_t inbytes;
size_t convResult;
...
//some loop and control code here
...
if (resbuff==NULL) {
resbuff=(char *)malloc(1024);
outbuff=(char *)malloc(1024);
}
I then call the library function to fill rebuff with data. Looking at the buffer in the debugger I can see the data in the buffer. For example, if the data is "test", I would see the following looking at the individual indexes of rebuff:
't','\0','e','\0','s','\0','t','\0'
Which I believe is UTF-16LE (other code using the same library would appear to confirm this), and stringlen now equals 8. I then try to convert that to UTF-8 using the following code:
iconv_t conv;
conv=iconv_open("UTF-8", "UTF-16LE");
inbytes=stringLen;
convResult=iconv(conv,&resbuff,&inbytes,&outbuff,&outbytes); //this does return 0
iconv_close(conv);
With the result that outbuff and resbuff both end up as null strings.
Note that I declare stringlen as an int rather than an unsigned long because that is what the library function is expecting.
EDIT: I tweaked my code slightly as per John Bollinger's answer below, but it didn't change the outcome.
EDIT 2: Ultimately the output from this code will be used in Python, so I'm thinking that while it might be uglier, I'll just perform the string conversion there. It just works.