I have an UTF-8
string and I need to get the byte array of UTF-16
encoding, so how can I convert my string to UTF-16
byte array?
Update:
I mean we have Encoding.Unicode.GetBytes()
or even Encoding.UTF8.GetBytes()
function to get byte array of strings, what about UTF-16
? We don't have any Encoding.UTF16.GetBytes()
so how can I get the byte array?
For little-endian UTF-16, use Encoding.Unicode
.
For big-endian UTF-16, use Encoding.BigEndianUnicode
.
Alternatively, construct an explicit instance of UnicodeEncoding
which allows you to specify the endianness, whether or not to include byte-order marks, and whether to throw an exception on invalid data.