user3749452
user3749452

Reputation: 43

UTF-16 to UTF-8 using ICU library

I wanted to convert UTF-16 strings to UTF-8. I came across the ICU library by Unicode. I am having problems doing the conversion as the default is UTF-16. I have tried using converter:

UErrorCode myError = U_ZERO_ERROR;
UConverter *conv = ucnv_open("UTF-8", &myError);
int32_t bytes = ucnv_fromUChars(conv,   target, 0, (UChar*)source, numread, &myError);
char *targetLimit = target + reqdLen;
const UChar *sourceLimit = mySrc + numread;
ucnv_fromUnicode(conv,&target, targetLimit, &mySrc, sourceLimit, NULL, TRUE, &myError);

I get bytes as -(big random number) and garbage at the original target location

What am i missing?

Upvotes: 4

Views: 1682

Answers (1)

Scott Russell
Scott Russell

Reputation: 76

It's a best practice to check for errors after calls that specify a UErrorCode parameter. I would start there.

Something like...

if (U_FAILURE(status)) 
{
    std::cout << "error: " << status << ":" << u_errorName(status) << std::endl;
}

Upvotes: 1

Related Questions