ASCIIEncoding.GetString Method (Byte[], Int32, Int32)


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

Namespace:   System.Text
Assembly:  mscorlib (in mscorlib.dll)

public override string GetString(
	byte[] bytes,
	int byteIndex,
	int byteCount


Type: System.Byte[]

The byte array containing the sequence of bytes to decode.

Type: System.Int32

The index of the first byte to decode.

Type: System.Int32

The number of bytes to decode.

Return Value

Type: System.String

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

Exception Condition

bytes is null.


index or count is less than zero.


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


A fallback occurred (see Character Encoding in the .NET Framework for complete explanation)


DecoderFallback is set to DecoderExceptionFallback.

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


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

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);

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

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top