The UTF-16 stream codecs implement this logic.
The UTF-16 encode and decode functions will however always strip the BOM mark from the beginning of a string.
If the application doesn’t want this stripping to happen, it should use the UTF-16-LE or -BE codec resp.—M.-A.