why UChar* is not working with this ICU conversion?
When converting from UTF-8 to ISO-8859-6 this code didn't work:
UnicodeString ustr = UnicodeString::fromUTF8(StringPiece(input));
const UChar* source = ustr.getBuffer();
char target[1000];
UErrorCode status = U_ZERO_ERR开发者_JS百科OR;
UConverter *conv;
int32_t len;
// set up the converter
conv = ucnv_open("iso-8859-6", &status);
assert(U_SUCCESS(status));
// convert
len = ucnv_fromUChars(conv, target, 100, source, -1, &status);
assert(U_SUCCESS(status));
// close the converter
ucnv_close(conv);
string s(target);
return s;
images: (1,2)
However when replacing UChar* with a hard-coded UChar[] it works well!! image : (3)
It looks like you're taking the difficult approach. How about this:
static char const* const cp = "iso-8859-6";
UnicodeString ustr = UnicodeString::fromUTF8(StringPiece(input));
std::vector<char> buf(ustr.length() + 1);
std::vector<char>::size_type len = ustr.extract(0, ustr.length(), &buf[0], buf.size(), cp);
if (len >= buf.size())
{
buf.resize(len + 1);
len = ustr.extract(0, ustr.length(), &buf[0], buf.size(), cp);
}
std::string ret;
if (len)
ret.assign(buf.begin(), buf.begin() + len));
return ret;
精彩评论