Olvasás angol nyelven Szerkesztés

Megosztás a következőn keresztül:


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 string GetString (byte[] bytes);

Parameters

bytes
Byte[]

Returns

Applies to

.NET Framework 1.1
Termék Verziók
.NET Framework 1.1

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 string GetString (byte[] bytes, int byteIndex, int byteCount);

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 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'

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 ("?").

Figyelemfelhívás

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

.NET 9 és más verziók
Termék Verziók
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0