Επεξεργασία

Κοινή χρήση μέσω


ASCIIEncoding.GetString Method

Definition

Overloads

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

Decodes a range of bytes from a byte array into a string.

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

Parameters

bytes
Byte[]

Returns

Applies to

GetString(Byte[], Int32, Int32)

Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs

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

Parameters

bytes
Byte[]

The byte array containing the sequence of bytes to decode.

byteIndex
Int32

The index of the first byte to decode.

byteCount
Int32

The number of bytes to decode.

Returns

A String containing the results of decoding the specified sequence of bytes.

Exceptions

bytes is null.

index or count is less than zero.

-or-

index and count do not denote a valid range in bytes.

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

DecoderFallback is set to DecoderExceptionFallback.

Examples

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'

Remarks

Data to be converted, such as data read from a stream, can be available only in sequential blocks. 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.

ASCIIEncoding does not provide error detection. Any byte greater than hexadecimal 0x7F is decoded as the Unicode question mark ("?").

Caution

For security reasons, you should use the UTF8Encoding, UnicodeEncoding, or UTF32Encoding classes and enable error detection instead of using the ASCIIEncoding class.

See also

Applies to