Encoding.GetString Method (Byte())
When overridden in a derived class, decodes all the bytes in the specified byte array into a string.
Assembly: mscorlib (in mscorlib.dll)
- Type: System.Byte()
The byte array containing the sequence of bytes to decode.
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, the application should use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively, of a derived class.
See the Remarks under Encoding.GetChars for more 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 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 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.
Imports System.IO Imports System.Text Module Example Const MAX_BUFFER_SIZE As Integer = 2048 Dim enc8 As Encoding = Encoding.UTF8 Public Sub Main() Dim fStream As New FileStream(".\Utf8Example.txt", FileMode.Open) Dim contents As String = Nothing ' If file size is small, read in a single operation. If fStream.Length <= MAX_BUFFER_SIZE Then Dim bytes(CInt(fStream.Length) - 1) As Byte fStream.Read(bytes, 0, bytes.Length) contents = enc8.GetString(bytes) ' If file size exceeds buffer size, perform multiple reads. Else contents = ReadFromBuffer(fStream) End If fStream.Close() Console.WriteLine(contents) End Sub Private Function ReadFromBuffer(fStream As FileStream) As String Dim bytes(MAX_BUFFER_SIZE) As Byte Dim output As String = String.Empty Dim decoder8 As Decoder = enc8.GetDecoder() Do While fStream.Position < fStream.Length Dim nBytes As Integer = fStream.Read(bytes, 0, bytes.Length) Dim nChars As Integer = decoder8.GetCharCount(bytes, 0, nBytes) Dim chars(nChars - 1) As Char nChars = decoder8.GetChars(bytes, 0, nBytes, chars, 0) output += New String(chars, 0, nChars) Loop Return output End Function End Module ' The example displays the following output: ' 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.
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.
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.