Encoding.GetString メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
派生クラスでオーバーライドされると、バイトシーケンスを文字列にデコードします。
オーバーロード
| 名前 | 説明 |
|---|---|
| GetString(Byte[], Int32, Int32) |
派生クラスでオーバーライドされると、指定したバイト配列のバイト シーケンスを文字列にデコードします。 |
| GetString(Byte*, Int32) |
派生クラスでオーバーライドされると、指定したアドレスから始まる指定したバイト数を文字列にデコードします。 |
| GetString(Byte[]) |
派生クラスでオーバーライドされると、指定したバイト配列内のすべてのバイトを文字列にデコードします。 |
| GetString(ReadOnlySpan<Byte>) |
派生クラスでオーバーライドされると、指定したバイト スパン内のすべてのバイトを文字列にデコードします。 |
GetString(Byte[], Int32, Int32)
派生クラスでオーバーライドされると、指定したバイト配列のバイト シーケンスを文字列にデコードします。
public:
virtual System::String ^ GetString(cli::array <System::Byte> ^ bytes, int index, int count);
public virtual string GetString(byte[] bytes, int index, int count);
abstract member GetString : byte[] * int * int -> string
override this.GetString : byte[] * int * int -> string
Public Overridable Function GetString (bytes As Byte(), index As Integer, count As Integer) As String
パラメーター
- bytes
- Byte[]
デコードするバイトシーケンスを含むバイト配列。
- index
- Int32
デコードする最初のバイトのインデックス。
- count
- Int32
デコードするバイト数。
返品
指定したバイト シーケンスをデコードした結果を含む文字列。
例外
バイト配列に無効な Unicode コード ポイントが含まれています。
bytes は nullです。
index または count が 0 未満です。
-または-
index および count は、 bytes内の有効な範囲を示していません。
フォールバックが発生しました (詳細については、「 .NET での文字エンコード」を参照してください)
および
DecoderFallback は DecoderExceptionFallback に設定されます。
例
次の例では、 FileStream オブジェクトで表されるバイナリ ファイルから UTF-8 でエンコードされた文字列を読み取ります。 2,048 バイト未満のファイルの場合、ファイル全体の内容をバイト配列に読み取り、 GetString(Byte[], Int32, Int32) メソッドを呼び出してデコードを実行します。 大きなファイルの場合は、一度に 2,048 バイトをバイト配列に読み込み、 Decoder.GetCharCount(Byte[], Int32, Int32) メソッドを呼び出して配列に含まれる文字の数を確認した後、 Decoder.GetChars(Byte[], Int32, Int32, Char[], Int32) メソッドを呼び出してデコードを実行します。
using System;
using System.IO;
using System.Text;
public class Example
{
const int MAX_BUFFER_SIZE = 2048;
static Encoding enc8 = Encoding.UTF8;
static byte[] bytes = new byte[MAX_BUFFER_SIZE];
public static void Main()
{
FileStream fStream = new FileStream(@".\Utf8Example.txt", FileMode.Open);
string contents = null;
// If file size is small, read in a single operation.
if (fStream.Length <= MAX_BUFFER_SIZE) {
int bytesRead = fStream.Read(bytes, 0, bytes.Length);
contents = enc8.GetString(bytes, 0, bytesRead);
}
// If file size exceeds buffer size, perform multiple reads.
else {
contents = ReadFromBuffer(fStream);
}
fStream.Close();
Console.WriteLine(contents);
}
private static string ReadFromBuffer(FileStream fStream)
{
string output = String.Empty;
Decoder decoder8 = enc8.GetDecoder();
while (fStream.Position < fStream.Length) {
int nBytes = fStream.Read(bytes, 0, bytes.Length);
int nChars = decoder8.GetCharCount(bytes, 0, nBytes);
char[] chars = new char[nChars];
nChars = decoder8.GetChars(bytes, 0, nBytes, chars, 0);
output += new String(chars, 0, nChars);
}
return output;
}
}
// 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.
Imports System.IO
Imports System.Text
Module Example
Const MAX_BUFFER_SIZE As Integer = 2048
Dim enc8 As Encoding = Encoding.UTF8
Dim bytes(MAX_BUFFER_SIZE -1) As Byte
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 bytesRead As Integer = fStream.Read(bytes, 0, bytes.Length)
contents = enc8.GetString(bytes, 0, bytesRead)
' 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.
この例では、次のテキストを使用します。このテキストは、Utf8Example.txtという名前の UTF-8 でエンコードされたファイルに保存する必要があります。
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.
注釈
変換するデータが連続したブロックでのみ使用できる場合 (ストリームから読み取られるデータなど)、またはデータの量が非常に大きくて小さいブロックに分割する必要がある場合は、派生クラスの Decoder メソッドまたは Encoder メソッドによって提供されるGetDecoderまたはGetEncoderをそれぞれ使用する必要があります。
デコードの手法と考慮事項については、 Encoding.GetChars リファレンス トピックの「解説」セクションを参照してください。
こちらもご覧ください
適用対象
GetString(Byte*, Int32)
重要
この API は CLS 準拠ではありません。
派生クラスでオーバーライドされると、指定したアドレスから始まる指定したバイト数を文字列にデコードします。
public:
System::String ^ GetString(System::Byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public string GetString(byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public string GetString(byte* bytes, int byteCount);
[System.CLSCompliant(false)]
public string GetString(byte* bytes, int byteCount);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
member this.GetString : nativeptr<byte> * int -> string
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.GetString : nativeptr<byte> * int -> string
[<System.CLSCompliant(false)>]
member this.GetString : nativeptr<byte> * int -> string
パラメーター
- bytes
- Byte*
バイト配列へのポインター。
- byteCount
- Int32
デコードするバイト数。
返品
指定したバイト シーケンスをデコードした結果を含む文字列。
- 属性
例外
bytes は null ポインターです。
byteCount が 0 未満です。
注釈
GetString メソッドは、バイト配列へのネイティブ ポインターがある場合のパフォーマンスを最適化するように設計されています。 マネージド バイト配列を作成してデコードする代わりに、中間オブジェクトを作成せずにこのメソッドを呼び出すことができます。
変換するデータがシーケンシャル ブロックでのみ使用できる場合 (ストリームから読み取られるデータなど)、またはデータの量が非常に大きいために小さなブロックに分割する必要がある場合は、派生クラスのGetDecoder メソッドによって返されるDecoder オブジェクトを使用する必要があります。
デコードの手法と考慮事項については、 Encoding.GetChars リファレンス トピックの「解説」セクションを参照してください。
特定のEncoding実装に対するGetStringメソッドの正確な動作は、そのEncoding オブジェクトに対して定義されているフォールバック戦略によって異なります。 詳細については、「.NETCharacter Encoding」トピックの「フォールバック戦略の選択>」セクションを参照してください。
こちらもご覧ください
適用対象
GetString(Byte[])
派生クラスでオーバーライドされると、指定したバイト配列内のすべてのバイトを文字列にデコードします。
public:
virtual System::String ^ GetString(cli::array <System::Byte> ^ bytes);
public virtual string GetString(byte[] bytes);
abstract member GetString : byte[] -> string
override this.GetString : byte[] -> string
Public Overridable Function GetString (bytes As Byte()) As String
パラメーター
- bytes
- Byte[]
デコードするバイトシーケンスを含むバイト配列。
返品
指定したバイト シーケンスをデコードした結果を含む文字列。
例外
バイト配列に無効な Unicode コード ポイントが含まれています。
bytes は nullです。
フォールバックが発生しました (詳細については、「 .NET での文字エンコード」を参照してください)
および
DecoderFallback は DecoderExceptionFallback に設定されます。
例
次の例では、 FileStream オブジェクトで表されるバイナリ ファイルから UTF-8 でエンコードされた文字列を読み取ります。 2,048 バイト未満のファイルの場合、ファイル全体の内容をバイト配列に読み取り、 GetString(Byte[]) メソッドを呼び出してデコードを実行します。 大きなファイルの場合は、一度に 2,048 バイトをバイト配列に読み込み、 Decoder.GetCharCount(Byte[], Int32, Int32) メソッドを呼び出して配列に含まれる文字の数を確認した後、 Decoder.GetChars(Byte[], Int32, Int32, Char[], Int32) メソッドを呼び出してデコードを実行します。
using System;
using System.IO;
using System.Text;
public class Example
{
const int MAX_BUFFER_SIZE = 2048;
static Encoding enc8 = Encoding.UTF8;
public static void Main()
{
FileStream fStream = new FileStream(@".\Utf8Example.txt", FileMode.Open);
string contents = null;
// If file size is small, read in a single operation.
if (fStream.Length <= MAX_BUFFER_SIZE) {
Byte[] bytes = new Byte[fStream.Length];
fStream.Read(bytes, 0, bytes.Length);
contents = enc8.GetString(bytes);
}
// If file size exceeds buffer size, perform multiple reads.
else {
contents = ReadFromBuffer(fStream);
}
fStream.Close();
Console.WriteLine(contents);
}
private static string ReadFromBuffer(FileStream fStream)
{
Byte[] bytes = new Byte[MAX_BUFFER_SIZE];
string output = String.Empty;
Decoder decoder8 = enc8.GetDecoder();
while (fStream.Position < fStream.Length) {
int nBytes = fStream.Read(bytes, 0, bytes.Length);
int nChars = decoder8.GetCharCount(bytes, 0, nBytes);
char[] chars = new char[nChars];
nChars = decoder8.GetChars(bytes, 0, nBytes, chars, 0);
output += new String(chars, 0, nChars);
}
return output;
}
}
// 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.
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.
この例では、次のテキストを使用します。このテキストは、Utf8Example.txtという名前の UTF-8 でエンコードされたファイルに保存する必要があります。
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.
注釈
変換するデータがシーケンシャル ブロックでのみ使用できる場合 (ストリームから読み取られるデータなど)、またはデータの量が非常に大きいために小さなブロックに分割する必要がある場合は、派生クラスのGetDecoder メソッドによって返されるDecoder オブジェクトを使用する必要があります。
デコードの手法と考慮事項については、 Encoding.GetChars リファレンス トピックの「解説」セクションを参照してください。
特定のEncoding実装に対するGetStringメソッドの正確な動作は、そのEncoding オブジェクトに対して定義されているフォールバック戦略によって異なります。 詳細については、「.NETCharacter Encoding」トピックの「フォールバック戦略の選択>」セクションを参照してください。
こちらもご覧ください
適用対象
GetString(ReadOnlySpan<Byte>)
派生クラスでオーバーライドされると、指定したバイト スパン内のすべてのバイトを文字列にデコードします。
public:
System::String ^ GetString(ReadOnlySpan<System::Byte> bytes);
public string GetString(ReadOnlySpan<byte> bytes);
member this.GetString : ReadOnlySpan<byte> -> string
Public Function GetString (bytes As ReadOnlySpan(Of Byte)) As String
パラメーター
- bytes
- ReadOnlySpan<Byte>
Unicode 文字列にデコードする読み取り専用バイト スパン。
返品
指定された読み取り専用スパンからデコードされたバイトを含む文字列。
注釈
GetStringメソッドは、パフォーマンスを最適化するように設計されています。 マネージド バイト配列を作成してデコードする代わりに、中間オブジェクトを作成せずにこのメソッドを呼び出すことができます。
変換するデータがシーケンシャル ブロックでのみ使用できる場合 (ストリームから読み取られるデータなど)、またはデータの量が非常に大きいために小さなブロックに分割する必要がある場合は、派生クラスのGetDecoder メソッドによって返されるDecoder オブジェクトを使用する必要があります。
デコードの手法と考慮事項については、 Encoding.GetChars リファレンス トピックの「解説」セクションを参照してください。
特定のEncoding実装に対するGetStringメソッドの正確な動作は、そのEncoding オブジェクトに対して定義されているフォールバック戦略によって異なります。 詳細については、「.NETCharacter Encoding」トピックの「フォールバック戦略の選択>」セクションを参照してください。