共用方式為


ASCIIEncoding.GetString 方法

定義

多載

GetString(Byte[])
GetString(Byte[], Int32, Int32)

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

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[]

傳回

適用於

GetString(Byte[], Int32, Int32)

來源:
ASCIIEncoding.cs
來源:
ASCIIEncoding.cs
來源:
ASCIIEncoding.cs

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

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

例外狀況

bytesnull

indexcount 小於零。

-或-

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

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

-和-

DecoderFallback 設定為 DecoderExceptionFallback

範例

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

using namespace System;
using namespace System::Text;

int main()
{
    // Define a string.
    String^ original = "ASCII Encoding Example";
    // Instantiate an ASCII encoding object.
    ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
    
    // Create an ASCII byte array.
    array<Byte>^ bytes = ascii->GetBytes(original); 
    
    // Display encoded bytes.
    Console::Write("Encoded bytes (in hex):  ");
    for each (Byte 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'
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 方法或 方法所提供的 GetDecoderGetEncoder

ASCIIEncoding 不提供錯誤偵測。 任何大於十六進位0x7F的位元組會譯碼為 Unicode 問號 (“?) 。

警告

基於安全性考慮,您應該使用 UTF8EncodingUnicodeEncodingUTF32Encoding 類別,並啟用錯誤偵測,而不是使用 ASCIIEncoding 類別。

另請參閱

適用於