Encoding: GetString |
When implemented in a derived class, decodes all the bytes in the specified byte array into a string.
Public Function GetString( ByRef Bytes ( ) As Byte, Optional ByRef Index As Variant, Optional ByRef Count As Variant ) As String
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.
Exception | Condition |
---|---|
ArgumentException | The byte array contains invalid Unicode code points. |
ArgumentNullException | Bytes is uninitialized. |
ArgumentOutOfRangeException |
Index is less than the lower-bound of Bytes. -or- Count is less than zero. -or- Index and Count do not denote a valid range in Bytes. |
DecoderFallbackException | A fallback occurred -and- DecoderFallback is set to DecoderExceptionFallback. |
The following example converts a string from one encoding to another.
Public Sub Main() Dim UnicodeString As String Dim ASCII As Encoding Dim Unicode As Encoding Dim UnicodeBytes() As Byte Dim ASCIIBytes() As Byte Dim ASCIIChars() As Integer Dim ASCIIString As String Set Console.OutputEncoding = Encoding.UTF8 UnicodeString = "This string contains the unicode character Pi (" & ChrW$(&H3A0) & ")" ' Create two different encodings. Set ASCII = Encoding.ASCII Set Unicode = Encoding.Unicode ' Convert the string into a byte array. UnicodeBytes = Unicode.GetBytes(UnicodeString) ' Perform the conversion from one encoding to the other. ASCIIBytes = Encoding.Convert(Unicode, ASCII, UnicodeBytes) ' Convert the new byte array into a string. ASCIIString = ASCII.GetString(ASCIIBytes) ' Display the strings created before and after the conversion. Console.WriteLine "Original string: {0}", UnicodeString Console.WriteLine "Ascii converted string: {0}", ASCIIString Console.ReadKey End Sub ' This example code produces the following output. ' ' Original string: This string contains the unicode character Pi (Π) ' Ascii converted string: This string contains the unicode character Pi (?)