Wildling Wildling - 2 months ago 38
C++ Question

UTF 16 to UTF8 using utf8 library in c++

I am using this library to do my conversions from UTF16 to UTF8 in C++.

The example suggests the following way to convert utf16 to utf8:

unsigned short utf16string[] = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e};
vector<unsigned char> utf8result;
utf16to8(utf16string, utf16string + 5, back_inserter(utf8result));
assert (utf8result.size() == 10);

where the definition of utf16to8 is given by:

template <typename u16bit_iterator, typename octet_iterator>
octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result);

I have a char array which has the characters in UTF16. Can you tell me if it is still possible to use this library if I do not know the size (number of valid characters) of my UTF16 character array ?


No. You cannot do anything meaningful with the data stored in a container of unknown size, obviously. You should know how many elements it holds.