Encoding::GetString Method (array<Byte>^, Int32, Int32)
When overridden in a derived class, decodes a sequence of bytes from the specified byte array into a string.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- bytes
-
Type:
array<System::Byte>^
The byte array containing the sequence of bytes to decode.
- index
-
Type:
System::Int32
The index of the first byte to decode.
- count
-
Type:
System::Int32
The number of bytes to decode.
Return Value
Type: System::String^A string that contains the results of decoding the specified sequence of bytes.
| Exception | Condition |
|---|---|
| ArgumentException | The byte array contains invalid Unicode code points. |
| 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. |
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 or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively, of a derived class.
See the Remarks section of the Encoding::GetChars reference topic for a discussion of decoding techniques and considerations.
The following example reads a UTF-8 encoded string from a binary file that is 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(array<Byte>^, Int32, Int32) method to perform the decoding. For larger files, it reads 2,048 bytes at a time into a byte array, calls the Decoder::GetCharCount(array<Byte>^, Int32, Int32) method to determine how many characters are contained in the array, and then calls the Decoder::GetChars(array<Byte>^, Int32, Int32, array<Char>^, Int32) method to perform the decoding.
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.
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1