ASCIIEncoding.GetString Metoda

Definice

Přetížení

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

String

Platí pro

GetString(Byte[], Int32, Int32)

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, který má dekódovat.

byteCount
Int32

Počet bajtů k dekódování

Návraty

String

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

Výjimky

bytes je null.

index nebo count je menší než nula.

-nebo- index a count neoznamujte platnou oblast v bytes.

Došlo k záložnímu obnovení (další informace najdete v tématu Kódování znaků v .NET). -a- DecoderFallback je nastavena na DecoderExceptionFallbackhodnotu .

Příklady

Následující příklad ukazuje, jak pomocí GetString metody převést bajtové pole na 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'

Poznámky

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

ASCIIEncoding neposkytuje detekci chyb. Jakýkoli bajt větší než šestnáctkový 0x7F je dekódován jako otazník Unicode ("?").

Upozornění

Z bezpečnostních důvodů byste měli místo použití třídy použít UTF8Encodingtřídu , UnicodeEncodingnebo UTF32Encoding třídy a povolit detekci ASCIIEncoding chyb.

Viz také

Platí pro