Can you please help me convert an UCS-2 string to UTF-8 using ICU ?
I'm using the following code, but it doesn't work.
UErrorCode status = U_ZERO_ERROR;
UConverter *conv;
char buf[1000];
int32_t buflen;
conv = ucnv_open("utf-8", &status);
if (U_FAILURE(status))
{
LOG(L_ERROR, "%s: Can not open the ICU converter\n", __FUNCTION__);
}
else
{
buflen = ucnv_fromUChars(conv, buf, sizeof(buf), (UChar*)sms->message.s, sms->message.len, &status);
if (U_FAILURE(status))
{
LOG(L_ERROR, "%s: Error in conversion: %s\n", __FUNCTION__, u_errorName(status));
}
}
LOG(L_DEBUG, "%s: Conversion made ...\n", __FUNCTION__);
hexdump(sms->message.s, sms->message.len);
hexdump(buf, buflen);
sms->message is a struct:
typedef struct str
{
char *s;
int len;
} str_t;
The hexdump prints the following (input text: "aaaa"):
[DEBUG] add_recv_sms_to_db: Conversion made ...
000000: 00 61 00 61 00 61 00 61 .a.a.a.a
000000: e6 84 80 e6 84 80 e6 84 80 e6 84 80 00 00 49 00 ..............I.