I have been looking around for some time for this question but always end up with something different.
I have the following UTF-32 string: std::u32string utf32s = U"जि";
And I would like to convert to an UnicodeString: UnicodeString ustr;
I am using the ICU 65.1 library in C++ to deal with Unicode String for normalization and composition, I found the following link which describe in a very poor way the conversion between strings. Especially the following description:
Conversion of whole strings:
u_strFromUTF32()
andu_strFromUTF32()
inustring.h
.Access to code points is trivial and does not require any macros.
Using a UTF-32 converter with all of the ICU conversion APIs in ucnv.h, including ones with an "Algorithmic" suffix.
UnicodeString has
fromUTF32()
andtoUTF32()
methods.
The alternative I have found is the following template function:
template <typename T>
void fromUTF32(const std::u32string& source, std::basic_string<T, std::char_traits<T>, std::allocator<T>>& result)
{
wstring_convert<codecvt_utf8_utf16<T>, T> convertor;
result = convertor.from_bytes(source);
}
This function anyhow seams not to recognize UnicodeString as valid input. More in general, given a string (wstring, string, u16string ...) how to create a template function to get it as a Unicode String ?
Many thanks !