.NET Framework Class Library 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.
Namespace:
System.Text
Assembly:
mscorlib (in mscorlib.dll)

Syntax
Public Overridable Function GetString ( _
bytes As Byte(), _
index As Integer, _
count As Integer _
) As String
public virtual string GetString(
byte[] bytes,
int index,
int count
)
public:
virtual String^ GetString(
array<unsigned char>^ bytes,
int index,
int count
)
abstract GetString :
bytes:byte[] *
index:int *
count:int -> string
override GetString :
bytes:byte[] *
index:int *
count:int -> string

Exceptions

Remarks
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.

Examples
The following example demonstrates how to read a UTF-8 encoded string from a binary file.
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
' This code produces the following output.
'
'Marin Millar
'BodyName: iso-8859-1
'HeaderName: Windows-1252
'WebName: Windows-1252
'CodePage: 1252
'EncodingName: Western European (Windows)
'WindowsCodePage: 1252
'MailNewsDisplay: True
'MailNewsSave: True
'BrowserDisplay: True
'BrowserSave: True
'
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);
}
/* This code produces the following output.
Marin Millar
BodyName: iso-8859-1
HeaderName: Windows-1252
WebName: Windows-1252
CodePage: 1252
EncodingName: Western European (Windows)
WindowsCodePage: 1252
MailNewsDisplay: True
MailNewsSave: True
BrowserDisplay: True
BrowserSave: True
*/
private:
String^ ReadAuthor( Stream^ binary_file )
{
System::Text::Encoding^ encoding = System::Text::Encoding::UTF8;
// Read string from binary file with UTF8 encoding
array<Byte>^ buffer = gcnew array<Byte>(30);
binary_file->Read( buffer, 0, 30 );
return encoding->GetString( buffer );
}
/* This code produces the following output.
Marin Millar
BodyName: iso-8859-1
HeaderName: Windows-1252
WebName: Windows-1252
CodePage: 1252
EncodingName: Western European (Windows)
WindowsCodePage: 1252
MailNewsDisplay: True
MailNewsSave: True
BrowserDisplay: True
BrowserSave: True
*/

Version Information
.NET FrameworkSupported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client ProfileSupported in: 4, 3.5 SP1 Portable Class LibrarySupported in: Portable Class Library

Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), 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.

See Also
|
.NET Framework クラス ライブラリ Encoding..::.GetString メソッド (array<Byte>[]()[], Int32, Int32) 派生クラスでオーバーライドされた場合、指定したバイト配列に格納されているバイト シーケンスを文字列にデコードします。
名前空間:
System.Text
アセンブリ:
mscorlib (mscorlib.dll 内)

構文
Public Overridable Function GetString ( _
bytes As Byte(), _
index As Integer, _
count As Integer _
) As String
public virtual string GetString(
byte[] bytes,
int index,
int count
)
public:
virtual String^ GetString(
array<unsigned char>^ bytes,
int index,
int count
)
abstract GetString :
bytes:byte[] *
index:int *
count:int -> string
override GetString :
bytes:byte[] *
index:int *
count:int -> string

例外

解説

例
次の例は、バイナリ ファイルから UTF-8 でエンコードされた文字列を読み取る方法を示しています。
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
' This code produces the following output.
'
'Marin Millar
'BodyName: iso-8859-1
'HeaderName: Windows-1252
'WebName: Windows-1252
'CodePage: 1252
'EncodingName: Western European (Windows)
'WindowsCodePage: 1252
'MailNewsDisplay: True
'MailNewsSave: True
'BrowserDisplay: True
'BrowserSave: True
'
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);
}
/* This code produces the following output.
Marin Millar
BodyName: iso-8859-1
HeaderName: Windows-1252
WebName: Windows-1252
CodePage: 1252
EncodingName: Western European (Windows)
WindowsCodePage: 1252
MailNewsDisplay: True
MailNewsSave: True
BrowserDisplay: True
BrowserSave: True
*/
private:
String^ ReadAuthor( Stream^ binary_file )
{
System::Text::Encoding^ encoding = System::Text::Encoding::UTF8;
// Read string from binary file with UTF8 encoding
array<Byte>^ buffer = gcnew array<Byte>(30);
binary_file->Read( buffer, 0, 30 );
return encoding->GetString( buffer );
}
/* This code produces the following output.
Marin Millar
BodyName: iso-8859-1
HeaderName: Windows-1252
WebName: Windows-1252
CodePage: 1252
EncodingName: Western European (Windows)
WindowsCodePage: 1252
MailNewsDisplay: True
MailNewsSave: True
BrowserDisplay: True
BrowserSave: True
*/

バージョン情報
.NET Frameworkサポート対象: 4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profileサポート対象: 4、3.5 SP1 サポート対象:

プラットフォーム
Windows 7, Windows Vista SP1 以降, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core はサポート対象外), Windows Server 2008 R2 (SP1 以降で Server Core をサポート), Windows Server 2003 SP2
.NET Framework では、各プラットフォームのすべてのバージョンはサポートしていません。 サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。

参照
|