1 out of 1 rated this helpful - Rate this topic

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
)

Parameters

bytes
Type: System.Byte[]
The byte array containing the sequence of bytes to decode.
byteIndex
Type: System.Int32
The index of the first byte to decode.
byteCount
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
ArgumentNullException

bytes is null.

ArgumentOutOfRangeException

index or count is less than zero.

-or-

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

DecoderFallbackException

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

-and-

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

Caution note Caution

For security reasons, your application is recommended to use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection.

The following example demonstrates how to use the GetString method to convert a byte array into a String.


using System;
using System.Text;

class ASCIIEncodingExample {
    public static void Main() {
        Byte[] bytes = new Byte[] {
             65,  83,  67,  73,  73,  32,  69,
            110,  99, 111, 100, 105, 110, 103,
             32,  69, 120,  97, 109, 112, 108, 101
        };

        ASCIIEncoding ascii = new ASCIIEncoding();
        String decoded = ascii.GetString(bytes);
        Console.WriteLine(decoded);
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ