UnicodeEncoding.GetBytes 方法 (System.Text) 您所在的位置:网站首页 0320什么星座 UnicodeEncoding.GetBytes 方法 (System.Text)

UnicodeEncoding.GetBytes 方法 (System.Text)

2023-11-07 20:53| 来源: 网络整理| 查看: 265

将指定字符数组中的一组字符编码到指定的字节数组中。

public: override int GetBytes(cli::array ^ chars, int charIndex, int charCount, cli::array ^ bytes, int byteIndex); public override int GetBytes (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex); override this.GetBytes : char[] * int * int * byte[] * int -> int Public Overrides Function GetBytes (chars As Char(), charIndex As Integer, charCount As Integer, bytes As Byte(), byteIndex As Integer) As Integer 参数 chars Char[]

包含要编码的字符集的字符数组。

charIndex Int32

第一个要编码的字符的索引。

charCount Int32

要编码的字符的数目。

bytes Byte[]

要包含所产生的字节序列的字节数组。

byteIndex Int32

要开始写入所产生的字节序列的索引位置。

返回 Int32

写入 bytes 的实际字节数。

例外 ArgumentNullException

chars 为 null (Nothing)。

或 - bytes 为 null (Nothing)。 ArgumentOutOfRangeException

charIndex、charCount 或 byteIndex 小于零。

或 - charIndex 和 charCount 不表示 chars中的有效范围。

或 - byteIndex 不是 bytes 中的有效索引。

ArgumentException

启用了错误检测,并且 chars 包含无效的字符序列。

或 - bytes 中从 byteIndex 到数组结尾没有足够的容量来容纳所产生的字节。 EncoderFallbackException

发生回退(有关详细信息,请参阅采用 .NET 的字符编码) -和- 将 EncoderFallback 设置为 EncoderExceptionFallback。

示例

下面的示例演示如何使用 GetBytes 该方法对字节数组中的元素范围中的字符 String 范围进行编码,并将编码的字节存储在字节数组中的一系列元素中。

using namespace System; using namespace System::Text; using namespace System::Collections; int main() { array^bytes; String^ chars = "Unicode Encoding Example"; UnicodeEncoding^ Unicode = gcnew UnicodeEncoding; int byteCount = Unicode->GetByteCount( chars->ToCharArray(), 8, 8 ); bytes = gcnew array(byteCount); int bytesEncodedCount = Unicode->GetBytes( chars, 8, 8, bytes, 0 ); Console::WriteLine( "{0} bytes used to encode string.", bytesEncodedCount ); Console::Write( "Encoded bytes: " ); IEnumerator^ myEnum = bytes->GetEnumerator(); while ( myEnum->MoveNext() ) { Byte b = safe_cast(myEnum->Current); Console::Write( "[{0}]", b ); } Console::WriteLine(); } using System; using System.Text; class UnicodeEncodingExample { public static void Main() { Byte[] bytes; String chars = "Unicode Encoding Example"; UnicodeEncoding Unicode = new UnicodeEncoding(); int byteCount = Unicode.GetByteCount(chars.ToCharArray(), 8, 8); bytes = new Byte[byteCount]; int bytesEncodedCount = Unicode.GetBytes(chars, 8, 8, bytes, 0); Console.WriteLine( "{0} bytes used to encode string.", bytesEncodedCount ); Console.Write("Encoded bytes: "); foreach (Byte b in bytes) { Console.Write("[{0}]", b); } Console.WriteLine(); } } Imports System.Text Class UnicodeEncodingExample Public Shared Sub Main() Dim bytes() As Byte Dim chars As String = "Unicode Encoding Example" Dim uni As New UnicodeEncoding() Dim byteCount As Integer = uni.GetByteCount(chars.ToCharArray(), 8, 8) bytes = New Byte(byteCount - 1) {} Dim bytesEncodedCount As Integer = uni.GetBytes(chars, 8, 8, bytes, 0) Console.WriteLine("{0} bytes used to encode string.", bytesEncodedCount) Console.Write("Encoded bytes: ") Dim b As Byte For Each b In bytes Console.Write("[{0}]", b) Next b Console.WriteLine() End Sub End Class 注解

若要计算存储生成的字节所需的 GetBytes 确切数组大小,请调用 GetByteCount 该方法。 若要计算最大数组大小,请调用 GetMaxByteCount 该方法。 该方法 GetByteCount 通常分配较少的内存,而 GetMaxByteCount 该方法通常执行速度更快。

通过错误检测,无效序列会导致此方法引发 ArgumentException。 如果没有错误检测,将忽略无效序列,并且不会引发异常。

要转换的数据(如从流中读取的数据)可能仅在顺序块中可用。 在这种情况下,或者如果数据量太大,因此需要将其划分为较小的块,则应用程序应分别使用DecoderEncoder方法或GetEncoder方法提供GetDecoder的数据量。

重要

为了确保编码的字节在保存为文件或流时正确解码,可以将编码的字节流作为前言前缀。 将谓词插入字节流开头 (,例如要写入文件一系列字节的开头,) 是开发人员的责任。 该方法 GetBytes 不会将谓词追加到编码字节序列的开头。

另请参阅 GetEncoder() GetByteCount(Char[], Int32, Int32) GetMaxByteCount(Int32) 适用于


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有