ASCIIEncoding.GetString 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| GetString(Byte[]) | |
| GetString(Byte[], Int32, Int32) | 将字节数组中某个范围的字节解码为一个字符串。 | 
GetString(Byte[])
GetString(Byte[], Int32, Int32)
- Source:
- ASCIIEncoding.cs
- Source:
- ASCIIEncoding.cs
- Source:
- ASCIIEncoding.cs
将字节数组中某个范围的字节解码为一个字符串。
public:
 override System::String ^ GetString(cli::array <System::Byte> ^ bytes, int byteIndex, int byteCount);public override string GetString(byte[] bytes, int byteIndex, int byteCount);override this.GetString : byte[] * int * int -> stringPublic Overrides Function GetString (bytes As Byte(), byteIndex As Integer, byteCount As Integer) As String参数
- bytes
- Byte[]
包含要解码的字节序列的字节数组。
- byteIndex
- Int32
第一个要解码的字节的索引。
- byteCount
- Int32
要解码的字节数。
返回
包含指定字节序列解码结果的 String。
例外
              bytes 为 null。
发生回退(有关详细信息,请参阅采用 .NET 的字符编码)
-和-
示例
下面的示例演示如何使用 GetString 方法将字节数组转换为 String。
using namespace System;
using namespace System::Text;
int main()
{
    // Define a string.
    String^ original = "ASCII Encoding Example";
    // Instantiate an ASCII encoding object.
    ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
    
    // Create an ASCII byte array.
    array<Byte>^ bytes = ascii->GetBytes(original); 
    
    // Display encoded bytes.
    Console::Write("Encoded bytes (in hex):  ");
    for each (Byte value in bytes)
       Console::Write("{0:X2} ", value);
    Console::WriteLine();
    // Decode the bytes and display the resulting Unicode string.
    String^ decoded = ascii->GetString(bytes);
    Console::WriteLine("Decoded string: '{0}'", decoded);
}
// The example displays the following output:
//   Encoded bytes (in hex):  41 53 43 49 49 20 45 6E 63 6F 64 69 6E 67 20 45 78 61 6D 70 6C 65
//   Decoded string: 'ASCII Encoding Example'
using System;
using System.Text;
class Example 
{
    public static void Main() 
    {
        // Define a string.
        String original = "ASCII Encoding Example";
        // Instantiate an ASCII encoding object.
        ASCIIEncoding ascii = new ASCIIEncoding();
        
        // Create an ASCII byte array.
        Byte[] bytes = ascii.GetBytes(original); 
        
        // Display encoded bytes.
        Console.Write("Encoded bytes (in hex):  ");
        foreach (var value in bytes)
           Console.Write("{0:X2} ", value);
        Console.WriteLine();
        // Decode the bytes and display the resulting Unicode string.
        String decoded = ascii.GetString(bytes);
        Console.WriteLine("Decoded string: '{0}'", decoded);
    }
}
// The example displays the following output:
//     Encoded bytes (in hex):  41 53 43 49 49 20 45 6E 63 6F 64 69 6E 67 20 45 78 61 6D 70 6C 65
//     Decoded string: 'ASCII Encoding Example'
Imports System.Text
Module Example
   
    Public Sub Main()
        ' Define a string.
        Dim original As String = "ASCII Encoding Example"
        ' Instantiate an ASCII encoding object.
        Dim ascii As New ASCIIEncoding()
        
        ' Create an ASCII byte array.
        Dim bytes() As Byte = ascii.GetBytes(original) 
        
        ' Display encoded bytes.
        Console.Write("Encoded bytes (in hex):  ")
        For Each value In bytes
           Console.Write("{0:X2} ", value)
        Next   
        Console.WriteLine()
        ' Decode the bytes and display the resulting Unicode string.
        Dim decoded As String = ascii.GetString(bytes)
        Console.WriteLine("Decoded string: '{0}'", decoded)
    End Sub
End Module
' The example displays the following output:
'   Encoded bytes (in hex):  41 53 43 49 49 20 45 6E 63 6F 64 69 6E 67 20 45 78 61 6D 70 6C 65
'   Decoded string: 'ASCII Encoding Example'
注解
要转换的数据(例如从流读取的数据)只能在顺序块中使用。 在这种情况下,或者如果数据量太大,需要将其划分为较小的块,应用程序应分别使用 Decoder 方法GetDecoder或 Encoder 方法提供的 或 GetEncoder 。
ASCIIEncoding 不提供错误检测。 任何大于十六进制0x7F的字节都解码为 Unicode 问号 (“?”) 。
注意
出于安全原因,应使用 UTF8Encoding、 UnicodeEncoding或 UTF32Encoding 类并启用错误检测,而不是使用 ASCIIEncoding 类。
另请参阅
- GetChars(Byte[], Int32, Int32, Char[], Int32)
- GetDecoder()
- GetCharCount(Byte[], Int32, Int32)
- GetMaxCharCount(Int32)