Encoding.GetString 方法

定義

在衍生類別中覆寫時,將位元組序列解碼成字串。

多載

GetString(Byte[])

在衍生類別中覆寫時,將指定位元組陣列中的所有位元組解碼成字串。

GetString(ReadOnlySpan<Byte>)

在衍生類別中覆寫時,將指定位元組範圍中的所有位元組解碼成字串。

GetString(Byte*, Int32)

在衍生類別中覆寫時,將指定位址開頭之指定數目的位元組解碼為字串。

GetString(Byte[], Int32, Int32)

在衍生類別中覆寫時,將指定位元組陣列中的位元組序列解碼成字串。

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[]

包含要解碼之位元組序列的位元組陣列。

傳回

String

字串,包含將指定之位元組序列解碼的結果。

例外狀況

位元組陣列包含無效的 Unicode 字碼指標。

bytesnull

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼)

-和-

DecoderFallback 設定為 DecoderExceptionFallback

範例

下列範例會從物件所代表的二進位檔案讀取 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 ,以取得解碼技巧和考慮的討論。

請注意, GetString 特定執行方法的確切行為 Encoding 取決於為該物件定義的回溯策略 Encoding 。 如需詳細資訊,請參閱 .net 主題中字元編碼 的「選擇回溯策略」一節。

另請參閱

適用於

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 字串的唯讀位元組範圍。

傳回

String

字串,其包含從所提供唯讀範圍解碼的位元組。

備註

GetString方法是設計來將效能優化。 您可以改為呼叫這個方法,而不需要建立任何中繼物件,而不是建立受控位元組陣列,然後將它解碼。

如果要轉換的資料僅適用于連續區塊 (例如從資料流程讀取的資料) 或者,如果資料量太大而必須分割成較小的區塊,您應該使用 Decoder 衍生類別的方法所傳回的物件 GetDecoder

請參閱參考主題的「備註」一節 Encoding.GetChars ,以取得解碼技巧和考慮的討論。

請注意, GetString 特定執行方法的確切行為 Encoding 取決於為該物件定義的回溯策略 Encoding 。 如需詳細資訊,請參閱 .net 主題中字元編碼 的「選擇回溯策略」一節。

適用於

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)]
public string GetString (byte* bytes, int byteCount);
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
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

要解碼的位元組數。

傳回

String

字串,包含將指定之位元組序列解碼的結果。

屬性

例外狀況

bytes 為 null 指標。

byteCount 小於零。

發生回退 (如需完整說明,請參閱 .net 中的字元編碼) )

-和-

DecoderFallback 設定為 DecoderExceptionFallback

備註

GetString當您的位元組陣列有原生指標時,此方法的設計目的是要將效能優化。 您可以改為呼叫這個方法,而不需要建立任何中繼物件,而不是建立受控位元組陣列,然後將它解碼。

如果要轉換的資料僅適用于連續區塊 (例如從資料流程讀取的資料) 或者,如果資料量太大而必須分割成較小的區塊,您應該使用 Decoder 衍生類別的方法所傳回的物件 GetDecoder

請參閱參考主題的「備註」一節 Encoding.GetChars ,以取得解碼技巧和考慮的討論。

請注意, GetString 特定執行方法的確切行為 Encoding 取決於為該物件定義的回溯策略 Encoding 。 如需詳細資訊,請參閱 .net 主題中字元編碼 的「選擇回溯策略」一節。

另請參閱

適用於

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

要解碼的位元組數。

傳回

String

字串,包含將指定之位元組序列解碼的結果。

例外狀況

位元組陣列包含無效的 Unicode 字碼指標。

bytesnull

indexcount 小於零。

-或-

indexcount 不代表 bytes 中有效的範圍。

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼)

-和-

DecoderFallback 設定為 DecoderExceptionFallback

範例

下列範例會從由物件所代表的二進位檔案讀取 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.

備註

如果要轉換的資料僅適用于連續區塊 (例如從資料流程讀取的資料) 或者,如果資料量太大而必須分割成較小的區塊,您應該 Decoder Encoder 分別使用方法或方法所提供的或作為 GetDecoder GetEncoder 衍生類別的。

請參閱參考主題的「備註」一節 Encoding.GetChars ,以取得解碼技巧和考慮的討論。

另請參閱

適用於