ASCIIEncoding.GetString 方法

定義

多載

名稱 Description
GetString(Byte[], Int32, Int32)

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

GetString(Byte[])

GetString(Byte[], Int32, Int32)

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

public:
 override System::String ^ GetString(cli::array <System::Byte> ^ bytes, int byteIndex, int byteCount);
public override string GetString(byte[] bytes, int byteIndex, int byteCount);
override this.GetString : byte[] * int * int -> string
Public Overrides Function GetString (bytes As Byte(), byteIndex As Integer, byteCount As Integer) As String

參數

bytes
Byte[]

包含要解碼的位元組序列的位元組陣列。

byteIndex
Int32

第一個要解碼的位元組的索引。

byteCount
Int32

需要解碼的位元組數。

傳回

String A 包含解碼指定位元組序列的結果。

例外狀況

bytesnull

byteIndexbyteCount 小於零。

-或-

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

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

-及-

DecoderFallback 設定為 DecoderExceptionFallback

範例

以下範例示範如何使用此 GetString 方法將位元組陣列轉換為 String

using System;
using System.Text;

class Example 
{
    public static void Main() 
    {
        // Define a string.
        String original = "ASCII Encoding Example";
        // Instantiate an ASCII encoding object.
        ASCIIEncoding ascii = new ASCIIEncoding();
        
        // Create an ASCII byte array.
        Byte[] bytes = ascii.GetBytes(original); 
        
        // Display encoded bytes.
        Console.Write("Encoded bytes (in hex):  ");
        foreach (var value in bytes)
           Console.Write("{0:X2} ", value);
        Console.WriteLine();

        // Decode the bytes and display the resulting Unicode string.
        String decoded = ascii.GetString(bytes);
        Console.WriteLine("Decoded string: '{0}'", decoded);
    }
}
// The example displays the following output:
//     Encoded bytes (in hex):  41 53 43 49 49 20 45 6E 63 6F 64 69 6E 67 20 45 78 61 6D 70 6C 65
//     Decoded string: 'ASCII Encoding Example'
Imports System.Text

Module Example
   
    Public Sub Main()
        ' Define a string.
        Dim original As String = "ASCII Encoding Example"
        ' Instantiate an ASCII encoding object.
        Dim ascii As New ASCIIEncoding()
        
        ' Create an ASCII byte array.
        Dim bytes() As Byte = ascii.GetBytes(original) 
        
        ' Display encoded bytes.
        Console.Write("Encoded bytes (in hex):  ")
        For Each value In bytes
           Console.Write("{0:X2} ", value)
        Next   
        Console.WriteLine()

        ' Decode the bytes and display the resulting Unicode string.
        Dim decoded As String = ascii.GetString(bytes)
        Console.WriteLine("Decoded string: '{0}'", decoded)
    End Sub
End Module
' The example displays the following output:
'   Encoded bytes (in hex):  41 53 43 49 49 20 45 6E 63 6F 64 69 6E 67 20 45 78 61 6D 70 6C 65
'   Decoded string: 'ASCII Encoding Example'

備註

待轉換的資料,例如從串流讀取的資料,只能以連續區塊形式提供。 在這種情況下,或資料量龐大到需要分割成較小區塊時,應用程式應分別使用DecoderEncoder方法或GetEncoder方法所提供的GetDecoder

ASCIIEncoding 無法提供錯誤偵測。 任何大於十六進位0x7F的位元組都會被解碼為 Unicode 問號(「?」)。

Caution

出於安全考量,你應該使用 UTF8EncodingUnicodeEncodingUTF32Encoding 類別,並啟用錯誤偵測功能,而不是使用類別 ASCIIEncoding

另請參閱

適用於

GetString(Byte[])

public:
 override System::String ^ GetString(cli::array <System::Byte> ^ bytes);
public override string GetString(byte[] bytes);
override this.GetString : byte[] -> string
Public Overrides Function GetString (bytes As Byte()) As String

參數

bytes
Byte[]

傳回

適用於