Expand/Shrink

utf32_to_utf16

Definition: dword_sequence utf16 = utf32_to_utf16(sequence utf32)
-- or --
dword_sequence utf16 = utf8_to_utf16(string utf8)
Description: Convert a UTF-32 sequence to UTF-16.

Returns a dword_sequence.
pwa/p2js: Not supported, mainly because I cannot think of any good reason to support utf16 in a browser 😀.
It would probably be fairly easy, apart from the fact that p2js.js (now) uses codePointAt() in preference to charCodeAt().
Comments: The input should not contain any elements outside the range 0..#10FFFF, or values in the range #D800..#DFFF (since that range is reserved, across the board, for UTF-16 surrogate pairs). Any such values are replaced with the element value #FFFD. Obviously the output can be chock-full of (matching) surrogate pairs.

Unlike utf32_to_utf8() there is no optional fail_flag, and when necessary you must manually check for any #FFFD in the return, rather than a return of -1.

Note, however, the input can legally contain #FFFD, which are returned unaltered, so technically it is more #FFFD in the output than were in the input that constitutes an error.

The function utf8_to_utf16() is a simple wrapper that returns utf32_to_utf16(utf8_to_utf32()).
See Also: utfconv, utf32_to_utf8, utf8_to_utf32, utf16_to_utf32