How to get UTF-16 byte array?

2019-01-27 22:25发布

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?

标签: c# encoding
2条回答
Ridiculous、
2楼-- · 2019-01-27 23:12

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.

查看更多
叛逆
3楼-- · 2019-01-27 23:16

I have an UTF-8 string and ...

No you don't. That's not possible. You may have a sequence (array or stream) of bytes that hold UTF-8 encoded text. But not a string.

A .net string always contains Unicode (or more precisely, UTF-16).

..., so how can I convert my string to UTF-16 byte array?

string myText = ...;  // some string, maybe from an UTF8 file or any other source
byte[] utf16Data = Encoding.Unicode.GetBytes(mytext);

The library defines the range UTF7, UTF8, Unicode, UTF32. Unicode is UTF16 in the context of the .NET framework.

查看更多
登录 后发表回答