This documentation is archived and is not being maintained.

Encoding.GetString Method

Decodes the specified byte array into a string.

Overload List

Returns a string containing the decoded representation of the specified byte array.

[Visual Basic] Overloads Public Overridable Function GetString(Byte()) As String
[C#] public virtual string GetString(byte[]);
[C++] public: virtual String* GetString(unsigned char __gc[]);
[JScript] public function GetString(Byte[]) : String;

Returns a string containing the decoded representation of a range of bytes in a byte array.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function GetString(Byte(), Integer, Integer) As String
[C#] public virtual string GetString(byte[], int, int);
[C++] public: virtual String* GetString(unsigned char __gc[], int, int);
[JScript] public function GetString(Byte[], int, int) : String;

Example

[Visual Basic, C#, C++] The following code example demonstrates how to read a UTF8 encoded string from binary file.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetString. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Private Function ReadAuthor(binary_file As Stream) As String
   Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
   ' Read string from binary file with UTF8 encoding
   Dim buffer(30) As Byte
   binary_file.Read(buffer, 0, 30)
   Return encoding.GetString(buffer)
End Function

[C#] 
private string ReadAuthor(Stream binary_file) {
  System.Text.Encoding encoding = System.Text.Encoding.UTF8;
  // Read string from binary file with UTF8 encoding
  byte[] buffer = new byte[30];
  binary_file.Read(buffer, 0, 30);
  return encoding.GetString(buffer);
}

[C++] 
String* ReadAuthor(Stream* binary_file) {
    System::Text::Encoding* encoding = System::Text::Encoding::UTF8;
    // Read string from binary file with UTF8 encoding
    Byte buffer[] = new Byte[30];
    binary_file->Read(buffer, 0, 30);
    return encoding->GetString(buffer);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Encoding Class | Encoding Members | System.Text Namespace

Show: