UTF7Encoding.GetString(Byte[], Int32, Int32) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将字节数组中的字节范围解码为字符串。
public:
override System::String ^ GetString(cli::array <System::Byte> ^ bytes, int index, int count);
public override string GetString(byte[] bytes, int index, int count);
[System.Runtime.InteropServices.ComVisible(false)]
public override string GetString(byte[] bytes, int index, int count);
override this.GetString : byte[] * int * int -> string
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetString : byte[] * int * int -> string
Public Overrides Function GetString (bytes As Byte(), index As Integer, count As Integer) As String
参数
- bytes
- Byte[]
包含要解码的字节序列的字节数组。
- index
- Int32
要解码的第一个字节的索引。
- count
- Int32
要解码的字节数。
返回
一个 String 包含解码指定字节序列的结果。
- 属性
例外
bytes 是 null (Nothing)。
发生回退(有关详细信息,请参阅 .NET 中的Character Encoding)。
-以及-
示例
下面的代码示例将字符串编码为字节数组,然后将字节解码回字符串。
using System;
using System.Text;
public class SamplesUTF7Encoding {
public static void Main() {
// Create an instance of UTF7Encoding.
UTF7Encoding u7 = new UTF7Encoding( true );
// Create byte arrays from the same string containing the following characters:
// Latin Small Letter Z (U+007A)
// Latin Small Letter A (U+0061)
// Combining Breve (U+0306)
// Latin Small Letter AE With Acute (U+01FD)
// Greek Small Letter Beta (U+03B2)
String myStr = "za\u0306\u01FD\u03B2";
// Encode the string.
byte[] myBArr = new byte[u7.GetByteCount( myStr )];
u7.GetBytes( myStr, 0, myStr.Length, myBArr, 0 );
// Decode the byte array.
Console.WriteLine( "The new string is: {0}", u7.GetString( myBArr, 0, myBArr.Length ) );
}
}
/*
This code produces the following output. The question marks take the place of characters that cannot be displayed at the console.
The new string is: za??
*/
Imports System.Text
Public Class SamplesUTF7Encoding
Public Shared Sub Main()
' Create an instance of UTF7Encoding.
Dim u7 As New UTF7Encoding(True)
' Create byte arrays from the same string containing the following characters:
' Latin Small Letter Z (U+007A)
' Latin Small Letter A (U+0061)
' Combining Breve (U+0306)
' Latin Small Letter AE With Acute (U+01FD)
' Greek Small Letter Beta (U+03B2)
Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2)
' Encode the string.
Dim myBArr(u7.GetByteCount(myStr)) As Byte
u7.GetBytes(myStr, 0, myStr.Length, myBArr, 0)
' Decode the byte array.
Console.WriteLine("The new string is: {0}", u7.GetString(myBArr, 0, myBArr.Length))
End Sub
End Class
'This code produces the following output. The question marks take the place of characters that cannot be displayed at the console.
'
'The new string is: za??ß
注解
要转换的数据(例如从流中读取的数据)可能仅在顺序块中可用。 在这种情况下,或者如果数据量太大,因此需要将其划分为较小的块,则应用程序应分别使用DecoderEncoder方法或GetEncoder方法提供GetDecoder的数据量。
注释
UTF7Encoding 不提供错误检测。 遇到无效字节时, UTF7Encoding 通常会发出无效字节。 如果字节大于十六进制0x7F,则字节值将零扩展到 Unicode 字符中,结果存储在数组中 chars ,并终止任何移位序列。 例如,如果要编码的字节是十六进制0x81,则生成的字符为 U+0081。 出于安全原因,建议 UTF8Encoding使用应用程序, UnicodeEncoding或 UTF32Encoding 启用错误检测。