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.No you don't. That's not possible. You may have a sequence (array or stream) of
byte
s that hold UTF-8 encoded text. But not astring
.A .net
string
always contains Unicode (or more precisely, UTF-16).The library defines the range UTF7, UTF8, Unicode, UTF32. Unicode is UTF16 in the context of the .NET framework.