ASCIIEncoding.GetString ASCIIEncoding.GetString ASCIIEncoding.GetString ASCIIEncoding.GetString Method

Definición

Sobrecargas

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

Descodifica un intervalo de bytes de una matriz de bytes en una cadena.Decodes a range of bytes from a byte array into a string.

GetString(Byte[]) GetString(Byte[]) GetString(Byte[]) 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

Parámetros

bytes
Byte[]

Devoluciones

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

Descodifica un intervalo de bytes de una matriz de bytes en una cadena.Decodes a range of bytes from a byte array into a string.

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

Parámetros

bytes
Byte[]

Matriz de bytes que contiene la secuencia de bytes que se va a descodificar.The byte array containing the sequence of bytes to decode.

byteIndex
Int32 Int32 Int32 Int32

Índice del primer byte que se va a descodificar.The index of the first byte to decode.

byteCount
Int32 Int32 Int32 Int32

Número de bytes que se van a descodificar.The number of bytes to decode.

Devoluciones

Objeto String que contiene los resultados obtenidos al descodificar la secuencia de bytes especificada.A String containing the results of decoding the specified sequence of bytes.

Excepciones

index o count es menor que cero.index or count is less than zero.

o bien-or- index y count no denotan un intervalo válido en bytes.index and count do not denote a valid range in bytes.

Se produjo una acción de reserva (consulte Codificación de caracteres en .NET Framework para obtener una explicación completa)A fallback occurred (see Character Encoding in the .NET Framework for complete explanation) - y --and- El valor de DecoderFallback está establecido en DecoderExceptionFallback.DecoderFallback is set to DecoderExceptionFallback.

Ejemplos

En el ejemplo siguiente se muestra cómo utilizar GetString el método para convertir una matriz de bytes Stringen.The following example demonstrates how to use the GetString method to convert a byte array into a 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'

Comentarios

Los datos que se van a convertir, como los datos leídos de una secuencia, solo pueden estar disponibles en bloques secuenciales.Data to be converted, such as data read from a stream, can be available only in sequential blocks. En este caso, o si la cantidad de datos es tan grande que debe dividirse en bloques más Decoder pequeños, la aplicación debe utilizar Encoder o proporcionado por el GetDecoder método o el GetEncoder método, respectivamente.In this case, or if the amount of data is so large that it needs to be divided into smaller blocks, the application should use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively.

ASCIIEncodingno proporciona detección de errores.ASCIIEncoding does not provide error detection. Cualquier byte mayor que el hexadecimal 0x7F se descodifica como el signo de interrogación Unicode ("?").Any byte greater than hexadecimal 0x7F is decoded as the Unicode question mark ("?").

Precaución

Por motivos de seguridad, debe usar las UTF8Encodingclases UnicodeEncoding, o UTF32Encoding y habilitar la detección de errores en lugar de utilizar ASCIIEncoding la clase.For security reasons, you should use the UTF8Encoding, UnicodeEncoding, or UTF32Encoding classes and enable error detection instead of using the ASCIIEncoding class.

Consulte también:

Se aplica a