Encoding.GetString メソッド

定義

派生クラスでオーバーライドされた場合、バイト シーケンスを文字列にデコードします。

オーバーロード

GetString(Byte[])

派生クラスでオーバーライドされた場合、指定したバイト配列に格納されているすべてのバイトを文字列にデコードします。

GetString(ReadOnlySpan<Byte>)

派生クラスでオーバーライドされた場合、指定したバイト スパンに格納されているすべてのバイトを文字列にデコードします。

GetString(Byte*, Int32)

派生クラスでオーバーライドされた場合、指定したアドレスで始まる指定したバイト数を文字列にデコードします。

GetString(Byte[], Int32, Int32)

派生クラスでオーバーライドされた場合、指定したバイト配列に格納されているバイト シーケンスを文字列にデコードします。

GetString(Byte[])

Source:
Encoding.cs
Source:
Encoding.cs
Source:
Encoding.cs

派生クラスでオーバーライドされた場合、指定したバイト配列に格納されているすべてのバイトを文字列にデコードします。

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 コード ポイントが含まれています。

bytesnullです。

フォールバックが発生しました (詳細については「.NET での文字エンコード」を参照)

および

DecoderFallbackDecoderExceptionFallback に設定されます。

次の例では、オブジェクトによって表されるバイナリファイルから、UTF-8 でエンコードされた文字列を読み取り FileStream ます。 2048バイトより小さいファイルの場合、ファイル全体の内容をバイト配列に読み取り、メソッドを呼び出してデコードを GetString(Byte[]) 実行します。 大きなファイルの場合は、一度に2048バイトをバイト配列に読み取り、メソッドを呼び出して 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.

注釈

変換するデータが連続したブロック (ストリームから読み取られたデータなど) でのみ使用可能な場合、またはデータ量が非常に大きく、小さいブロックに分割する必要がある場合は、 Decoder 派生クラスのメソッドによって返されたオブジェクトを使用する必要があり GetDecoder ます。

Encoding.GetCharsデコード手法と考慮事項の説明については、リファレンストピックの「解説」を参照してください。

特定の実装に対するメソッドの正確な動作は、 GetStringEncoding そのオブジェクトに対して定義されているフォールバックストラテジによって異なり Encoding ます。 詳細については、「 .net での文字エンコード」トピックの「フォールバックストラテジの選択」セクションを参照してください。

こちらもご覧ください

適用対象

GetString(ReadOnlySpan<Byte>)

Source:
Encoding.cs
Source:
Encoding.cs
Source:
Encoding.cs

派生クラスでオーバーライドされた場合、指定したバイト スパンに格納されているすべてのバイトを文字列にデコードします。

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メソッドは、パフォーマンスを最適化するように設計されています。 マネージバイト配列を作成してからデコードする代わりに、このメソッドを呼び出すことができます。その際、中間オブジェクトを作成する必要はありません。

変換するデータが連続したブロック (ストリームから読み取られたデータなど) でのみ使用可能な場合、またはデータ量が非常に大きく、小さいブロックに分割する必要がある場合は、 Decoder 派生クラスのメソッドによって返されたオブジェクトを使用する必要があり GetDecoder ます。

Encoding.GetCharsデコード手法と考慮事項の説明については、リファレンストピックの「解説」を参照してください。

特定の実装に対するメソッドの正確な動作は、 GetStringEncoding そのオブジェクトに対して定義されているフォールバックストラテジによって異なり Encoding ます。 詳細については、「 .net での文字エンコード」トピックの「フォールバックストラテジの選択」セクションを参照してください。

適用対象

GetString(Byte*, Int32)

Source:
Encoding.cs
Source:
Encoding.cs
Source:
Encoding.cs

重要

この 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)]
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)>]
[<System.Security.SecurityCritical>]
member this.GetString : nativeptr<byte> * int -> string
[<System.CLSCompliant(false)>]
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

パラメーター

bytes
Byte*

バイト配列へのポインター。

byteCount
Int32

デコードするバイト数。

戻り値

指定したバイト シーケンスのデコード結果が格納されている文字列。

属性

例外

bytes が null ポインターです。

byteCount が 0 未満です。

フォールバックが発生しました (詳細な説明については「.NET での文字エンコード」を参照)

および

DecoderFallbackDecoderExceptionFallback に設定されます。

注釈

メソッドは、 GetString バイト配列へのネイティブポインターがある場合にパフォーマンスを最適化するように設計されています。 マネージバイト配列を作成してからデコードする代わりに、このメソッドを呼び出すことができます。その際、中間オブジェクトを作成する必要はありません。

変換するデータが連続したブロック (ストリームから読み取られたデータなど) でのみ使用可能な場合、またはデータ量が非常に大きく、小さいブロックに分割する必要がある場合は、 Decoder 派生クラスのメソッドによって返されたオブジェクトを使用する必要があり GetDecoder ます。

Encoding.GetCharsデコード手法と考慮事項の説明については、リファレンストピックの「解説」を参照してください。

特定の実装に対するメソッドの正確な動作は、 GetStringEncoding そのオブジェクトに対して定義されているフォールバックストラテジによって異なり Encoding ます。 詳細については、「 .net での文字エンコード」トピックの「フォールバックストラテジの選択」セクションを参照してください。

こちらもご覧ください

適用対象

GetString(Byte[], Int32, Int32)

Source:
Encoding.cs
Source:
Encoding.cs
Source:
Encoding.cs

派生クラスでオーバーライドされた場合、指定したバイト配列に格納されているバイト シーケンスを文字列にデコードします。

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 コード ポイントが含まれています。

bytesnullです。

index または count が 0 未満です。

または

index および countbytes において有効な範囲を表していません。

フォールバックが発生しました (詳細については「.NET での文字エンコード」を参照)

および

DecoderFallbackDecoderExceptionFallback に設定されます。

次の例では、オブジェクトによって表されるバイナリファイルから、UTF-8 でエンコードされた文字列を読み取り FileStream ます。 2048バイトより小さいファイルの場合、ファイル全体の内容をバイト配列に読み取り、メソッドを呼び出してデコードを GetString(Byte[], Int32, Int32) 実行します。 大きなファイルの場合は、一度に2048バイトをバイト配列に読み取り、メソッドを呼び出して 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.

注釈

変換するデータが、連続したブロック (ストリームから読み取られたデータなど) でのみ使用可能な場合、またはデータの量が大きく、小さいブロックに分割する必要がある場合は、 DecoderEncoderGetDecoder 派生クラスのメソッドまたはメソッドによって提供されるまたはを使用する必要があり GetEncoder ます。

Encoding.GetCharsデコード手法と考慮事項の説明については、リファレンストピックの「解説」を参照してください。

こちらもご覧ください

適用対象