ASCIIEncoding.GetChars 方法

定义

将一个字节序列解码为一组字符。

重载

GetChars(ReadOnlySpan<Byte>, Span<Char>)

将指定的字节范围解码为指定的字符范围。

GetChars(Byte*, Int32, Char*, Int32)

将从指定的字节指针开始的一个字节序列解码为一组字符,并从指定的字符指针开始存储这组字符。

GetChars(Byte[], Int32, Int32, Char[], Int32)

将指定字节数组中的一个字节序列解码为指定的字符数组。

GetChars(ReadOnlySpan<Byte>, Span<Char>)

Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs

将指定的字节范围解码为指定的字符范围。

C#
public override int GetChars (ReadOnlySpan<byte> bytes, Span<char> chars);

参数

bytes
ReadOnlySpan<Byte>

包含要解码的字节的范围。

chars
Span<Char>

要包含生成的字符集的范围。

返回

写入 chars 的实际字符数。

注解

若要计算存储生成的字符所需的 GetChars 确切大小,请使用 GetCharCount。 若要计算最大大小,请使用 GetMaxCharCountGetCharCount方法通常允许分配较少的内存,而 GetMaxCharCount 方法的执行速度通常更快。

要转换的数据(例如从流读取的数据)只能在顺序块中使用。 在这种情况下,或者如果数据量太大,需要将其划分为较小的块,则应分别使用 Decoder 方法或 Encoder 方法提供的 GetDecoderGetEncoder

ASCIIEncoding 不提供错误检测。 任何大于十六进制的 0x7F 字节都解码为 Unicode 问号 (“?”) 。

注意

出于安全原因,应使用 UTF8EncodingUnicodeEncodingUTF32Encoding 并启用错误检测。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9

GetChars(Byte*, Int32, Char*, Int32)

Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs

重要

此 API 不符合 CLS。

符合 CLS 的替代方案
System.Text.ASCIIEncoding.GetChars(Byte[], Int32, Int32, Char[], Int32)

将从指定的字节指针开始的一个字节序列解码为一组字符,并从指定的字符指针开始存储这组字符。

C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetChars (byte* bytes, int byteCount, char* chars, int charCount);
C#
[System.CLSCompliant(false)]
public override int GetChars (byte* bytes, int byteCount, char* chars, int charCount);
C#
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetChars (byte* bytes, int byteCount, char* chars, int charCount);
C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetChars (byte* bytes, int byteCount, char* chars, int charCount);

参数

bytes
Byte*

指向第一个要解码的字节的指针。

byteCount
Int32

要解码的字节数。

chars
Char*

一个指针,指向开始写入所产生的字符集的位置。

charCount
Int32

要写入的最大字符数。

返回

在由 chars 指示的位置处写入的实际字符数。

属性

例外

bytesnull

charsnull

byteCountcharCount 小于零。

charCount 少于所产生的字符数。

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

-和-

DecoderFallback 设置为 DecoderExceptionFallback

注解

为了计算存储生成的字符所需的 GetChars 确切数组大小,应用程序使用 GetCharCount。 若要计算最大数组大小,应用程序应使用 GetMaxCharCountGetCharCount方法通常允许分配较少的内存,而 GetMaxCharCount 方法的执行速度通常更快。

要转换的数据(例如从流读取的数据)只能在顺序块中使用。 在这种情况下,或者如果数据量太大,需要将其划分为较小的块,应用程序应分别使用 Decoder 方法GetDecoderEncoder 方法提供的 或 GetEncoder

ASCIIEncoding 不提供错误检测。 任何大于十六进制0x7F的字节都解码为 Unicode 问号 (“?”) 。

注意

出于安全原因,建议应用程序使用 UTF8EncodingUnicodeEncodingUTF32Encoding 并启用错误检测。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetChars(Byte[], Int32, Int32, Char[], Int32)

Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs
Source:
ASCIIEncoding.cs

将指定字节数组中的一个字节序列解码为指定的字符数组。

C#
public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex);

参数

bytes
Byte[]

包含要解码的字节序列的字节数组。

byteIndex
Int32

第一个要解码的字节的索引。

byteCount
Int32

要解码的字节数。

chars
Char[]

要用于包含所产生的字符集的字符数组。

charIndex
Int32

开始写入所产生的字符集的索引位置。

返回

写入 chars 的实际字符数。

例外

bytesnull

charsnull

byteIndexbyteCountcharIndex 小于零。

byteindexbyteCount 不表示 bytes中的有效范围。

charIndex 不是 chars 中的有效索引。

chars 中从 charIndex 到数组结尾没有足够容量来容纳所产生的字符。

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

-和-

DecoderFallback 设置为 DecoderExceptionFallback

示例

下面的示例演示如何解码字节数组中的元素范围,并将结果存储在 Unicode 字符数组中的一组元素中。

C#
using System;
using System.Text;

class ASCIIEncodingExample {
    public static void Main() {
        Char[] chars;
        Byte[] bytes = new Byte[] {
             65,  83,  67,  73,  73,  32,  69,
            110,  99, 111, 100, 105, 110, 103,
             32,  69, 120,  97, 109, 112, 108, 101
        };

        ASCIIEncoding ascii = new ASCIIEncoding();

        int charCount = ascii.GetCharCount(bytes, 6, 8);
        chars = new Char[charCount];
        int charsDecodedCount = ascii.GetChars(bytes, 6, 8, chars, 0);

        Console.WriteLine(
            "{0} characters used to decode bytes.", charsDecodedCount
        );

        Console.Write("Decoded chars: ");
        foreach (Char c in chars) {
            Console.Write("[{0}]", c);
        }
        Console.WriteLine();
    }
}

注解

为了计算存储生成的字符所需的 GetChars 确切数组大小,应用程序使用 GetCharCount。 若要计算最大数组大小,应用程序应使用 GetMaxCharCountGetCharCount方法通常允许分配较少的内存,而 GetMaxCharCount 方法的执行速度通常更快。

要转换的数据(例如从流读取的数据)只能在顺序块中使用。 在这种情况下,或者如果数据量太大,需要将其划分为较小的块,应用程序应分别使用 Decoder 方法GetDecoderEncoder 方法提供的 或 GetEncoder

ASCIIEncoding 不提供错误检测。 任何大于十六进制0x7F的字节都解码为 Unicode 问号 (“?”) 。

注意

出于安全原因,建议应用程序使用 UTF8EncodingUnicodeEncodingUTF32Encoding 并启用错误检测。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0