ASCIIEncoding.GetString 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| 名称 | 说明 |
|---|---|
| GetString(Byte[]) | |
| GetString(Byte[], Int32, Int32) |
将字节数组中的字节范围解码为字符串。 |
GetString(Byte[], Int32, Int32)
- Source:
- ASCIIEncoding.cs
- Source:
- ASCIIEncoding.cs
- 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 -> string
Public 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 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'
注解
要转换的数据(例如从流中读取的数据)只能在顺序块中使用。 在这种情况下,或者如果数据量太大,因此需要将其划分为较小的块,则应用程序应分别使用DecoderEncoder方法或GetDecoder方法提供GetEncoder的数据量。
ASCIIEncoding 不提供错误检测。 任何大于十六进制0x7F的字节都解码为 Unicode 问号(“?)。
Caution
出于安全原因,应使用UTF8Encoding或UnicodeEncodingUTF32Encoding类并启用错误检测,而不是使用ASCIIEncoding类。
另请参阅
- GetChars(Byte[], Int32, Int32, Char[], Int32)
- GetDecoder()
- GetCharCount(Byte[], Int32, Int32)
- GetMaxCharCount(Int32)