Encoding.GetString 方法

定义

在派生类中重写时,将字节序列解码为字符串。

重载

名称 说明
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 码位。

bytesnull

indexcount 小于零。

-或-

indexcount不表示有效范围。bytes

发生回退(有关详细信息,请参阅 .NET 中的字符编码

-以及-

DecoderFallback 设置为 DecoderExceptionFallback

示例

以下示例从对象表示的二进制文件中读取 UTF-8 编码的 FileStream 字符串。 对于小于 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方法中的EncoderGetDecoder方法中的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 小于零。

发生回退(有关完整说明,请参阅 .NET>Character Encoding)

-以及-

DecoderFallback 设置为 DecoderExceptionFallback

注解

当具有指向字节数组的本机指针时,该方法 GetString 旨在优化性能。 可以改为调用此方法,而无需创建任何中间对象,而不是创建托管字节数组并对其进行解码。

如果要转换的数据仅在顺序块(如从流中读取的数据)中可用,或者如果数据量太大,因此需要将其划分为较小的块,则应使用 Decoder 派生类的方法返回 GetDecoder 的对象。

有关解码技术和注意事项的讨论,请参阅参考主题的 Encoding.GetChars “备注”部分。

请注意,特定Encoding实现方法的GetString精确行为取决于为该Encoding对象定义的回退策略。 有关详细信息,请参阅 .NET 主题中 Character 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 码位。

bytesnull

发生回退(有关详细信息,请参阅 .NET 中的字符编码

-以及-

DecoderFallback 设置为 DecoderExceptionFallback

示例

以下示例从对象表示的二进制文件中读取 UTF-8 编码的 FileStream 字符串。 对于小于 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.

注解

如果要转换的数据仅在顺序块(如从流中读取的数据)中可用,或者如果数据量太大,因此需要将其划分为较小的块,则应使用 Decoder 派生类的方法返回 GetDecoder 的对象。

有关解码技术和注意事项的讨论,请参阅参考主题的 Encoding.GetChars “备注”部分。

请注意,特定Encoding实现方法的GetString精确行为取决于为该Encoding对象定义的回退策略。 有关详细信息,请参阅 .NET 主题中 Character 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 旨在优化性能。 可以改为调用此方法,而无需创建任何中间对象,而不是创建托管字节数组并对其进行解码。

如果要转换的数据仅在顺序块(如从流中读取的数据)中可用,或者如果数据量太大,因此需要将其划分为较小的块,则应使用 Decoder 派生类的方法返回 GetDecoder 的对象。

有关解码技术和注意事项的讨论,请参阅参考主题的 Encoding.GetChars “备注”部分。

请注意,特定Encoding实现方法的GetString精确行为取决于为该Encoding对象定义的回退策略。 有关详细信息,请参阅 .NET 主题中 Character Encoding 的“选择回退策略”部分。

适用于