UTF7Encoding.GetString(Byte[], Int32, Int32) 方法

定義

將位元組陣列中的位元組範圍解碼成字串。

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 A 包含解碼指定位元組序列的結果。

屬性

例外狀況

bytesnullNothing)。

indexcount 小於零。

-或-

indexcount 不表示在 中的 bytes有效範圍。

隨後出現了備用機制(更多資訊請參見 .NET 中的字元編碼)。

-及-

DecoderFallback 設定為 DecoderExceptionFallback

範例

以下程式碼範例將一個字串編碼成一個位元組陣列,然後再將位元組解碼回字串。

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

Note

UTF7Encoding 無法提供錯誤偵測。 遇到無效位元組時, UTF7Encoding 通常會發出無效位元組。 若位元組大於十六進位0x7F,該位元組值會被零擴展成 Unicode 字元,結果會儲存在 chars 陣列中,並終止任何移位序列。 例如,若編碼的位元組為十六進位0x81,所得字元為 U+0081。 出於安全考量,建議您的應用程式使用 UTF8Encoding、 , UnicodeEncodingUTF32Encoding 啟用錯誤偵測功能。

適用於

另請參閱