Encoding.GetString Method (Byte[])

When overridden in a derived class, decodes all the bytes in the specified byte array into a string.

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

abstract GetString : 
        bytes:byte[] -> string  
override GetString : 
        bytes:byte[] -> string

Parameters

bytes
Type: System.Byte[]

The byte array containing the sequence of bytes to decode.

Return Value

Type: System.String
A string that contains the results of decoding the specified sequence of bytes.

ExceptionCondition
ArgumentException

The byte array contains invalid Unicode code points.

ArgumentNullException

bytes is a null reference (Nothing in Visual Basic).

DecoderFallbackException

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

-and-

DecoderFallback is set to DecoderExceptionFallback.

If the data to be converted is available only in sequential blocks (such as data read from a stream) or if the amount of data is so large that it needs to be divided into smaller blocks, you should use the Decoder object returned by the GetDecoder method of a derived class.

See the Remarks section of the Encoding.GetChars reference topic for a discussion of decoding techniques and considerations.

Note that the precise behavior of the GetString method for a particular Encoding implementation depends on the fallback strategy defined for that Encoding object. For more information, see the "Choosing a Fallback Strategy" section of the Character Encoding in the .NET Framework topic.

The following example reads a UTF-8 encoded string from a binary file represented by a FileStream object. For files that are smaller than 2,048 bytes, it reads the contents of the entire file into a byte array and calls the GetString(Byte[]) method to perform the decoding. For larger files, it reads 2,048 bytes at a time into a byte array, calls the Decoder.GetCharCount(Byte[], Int32, Int32) method to determine how many characters are contained in the array, and then calls the Decoder.GetChars(Byte[], Int32, Int32, Char[], Int32) method to perform the decoding.

No code example is currently available or this language may not be supported.

The example uses the following text, which should be saved to a UTF-8 encoded file named Utf8Example.txt.

This is a UTF-8-encoded file that contains primarily Latin text, although it 
does list the first twelve letters of the Russian (Cyrillic) alphabet:

А б в г д е ё ж з и й к

The goal is to save this file, then open and decode it as a binary stream.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft