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 的字符编码

-和-

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 了解解码方法和注意事项。

请注意,特定实现的方法的精确行为 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 小于零。

发生了回退(如需完整说明,请参阅 .NET 中的字符编码

-和-

DecoderFallback 设置为 DecoderExceptionFallback

注解

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

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.

注解

如果要转换的数据仅在顺序块(如从流中读取的数据)中可用,或者如果数据量很大以致需要分为更小块,则应使用 DecoderEncoderGetDecoder 派生类的方法或方法提供的或 GetEncoder

请参阅参考主题的 "备注" 部分, Encoding.GetChars 了解解码方法和注意事项。

另请参阅

适用于