如何获得UTF-16字节数组? [英] How to get UTF-16 byte array?

查看:68
本文介绍了如何获得UTF-16字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UTF-8 字符串,我需要获取 UTF-16 编码的字节数组,因此如何将我的字符串转换为 UTF-16 字节数组?

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?

更新:
我的意思是我们有 Encoding.Unicode.GetBytes()甚至是 Encoding.UTF8.GetBytes()函数来获取字符串的字节数组,而 UTF-16呢??我们没有任何 Encoding.UTF16.GetBytes(),所以如何获取字节数组?

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?

推荐答案

对于小端UTF-16,请使用

For little-endian UTF-16, use Encoding.Unicode.

对于Big-endian UTF-16,请使用 Encoding.BigEndianUnicode .

For big-endian UTF-16, use Encoding.BigEndianUnicode.

或者,构造一个 UnicodeEncoding ,它允许您指定字节序,是否包括字节序标记以及是否对无效数据引发异常.

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.

这篇关于如何获得UTF-16字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆