共用方式為


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

屬性

例外狀況

bytesnull (Nothing)。

indexcount 小於零。

-或-

indexcount 不代表 bytes 中有效的範圍。

如需詳細資訊, (發生後援,請參閱 .NET) 中的字元編碼

-和-

DecoderFallback 設定為 DecoderExceptionFallback

範例

下列程式代碼範例會將字串編碼為位元組數位,然後將位元組譯碼回字串。

using namespace System;
using namespace System::Text;
int main()
{
   
   // Create an instance of UTF7Encoding.
   UTF7Encoding^ u7 = gcnew 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.
   array<Byte>^myBArr = gcnew array<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??

*/
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??ß

備註

要轉換的數據,例如從數據流讀取的數據,可能只能在循序區塊中使用。 在此情況下,或者,如果數據量太大,因此需要分成較小的區塊,應用程式應該分別使用 Decoder 方法Encoder或 方法所提供的 GetDecoderGetEncoder

注意

UTF7Encoding 不提供錯誤偵測。 遇到無效的位元組時, UTF7Encoding 通常會發出無效的位元組。 如果位元組大於十六進位0x7F,位元組值會以零延伸至 Unicode 字元,結果會儲存在陣列中 chars ,並終止任何移位序列。 例如,如果要編碼的位元組是十六進位0x81,則產生的字元為U+0081。 基於安全性考慮,建議您使用 UTF8EncodingUnicodeEncodingUTF32Encoding ,並啟用錯誤偵測。

適用於

另請參閱