ASCIIEncoding.GetString Metoda

Definice

Přetížení

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

Dekóduje rozsah bajtů z bajtového pole do řetězce.

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

Parametry

bytes
Byte[]

Návraty

Platí pro

GetString(Byte[], Int32, Int32)

Zdroj:
ASCIIEncoding.cs
Zdroj:
ASCIIEncoding.cs
Zdroj:
ASCIIEncoding.cs
Zdroj:
ASCIIEncoding.cs
Zdroj:
ASCIIEncoding.cs

Dekóduje rozsah bajtů z bajtového pole do řetězce.

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

Parametry

bytes
Byte[]

Bajtové pole obsahující posloupnost bajtů k dekódování.

byteIndex
Int32

Index prvního bajtu pro dekódování.

byteCount
Int32

Počet bajtů, které se mají dekódovat.

Návraty

A String obsahující výsledky dekódování zadané sekvence bajtů.

Výjimky

bytes je null.

byteIndex nebo byteCount je menší než nula.

nebo

byteIndex a byteCount neoznamujte platný rozsah v bytes.

Došlo k záložnímu použití (další informace najdete v tématu Kódování znaků v .NET).

a

DecoderFallback je nastaveno na DecoderExceptionFallback.

Příklady

Následující příklad ukazuje, jak použít metodu GetString k převodu bajtového pole na .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'

Poznámky

Data, která se mají převést, například data načtená ze streamu, mohou být k dispozici pouze v sekvenčních blocích. V tomto případě nebo pokud je množství dat tak velké, že je potřeba je rozdělit na menší bloky, měla by aplikace použít Decoder metodu Encoder nebo GetDecoder metodu nebo metoduGetEncoder.

ASCIIEncoding neposkytuje detekci chyb. Libovolný bajt větší než šestnáctková 0x7F se dekóduje jako otazník Unicode ("?").

Viz také

Platí pro