UnicodeEncoding.GetByteCount 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
计算对一组字符进行编码时产生的字节数。
重载
GetByteCount(String) |
计算对指定字符串中的字符进行编码时所产生的字节数。 |
GetByteCount(Char*, Int32) |
计算对从指定的字符指针开始的一组字符进行编码时产生的字节数。 |
GetByteCount(Char[], Int32, Int32) |
计算对指定字符数组中的一组字符进行编码时产生的字节数。 |
GetByteCount(String)
计算对指定字符串中的字符进行编码时所产生的字节数。
public:
override int GetByteCount(System::String ^ s);
public override int GetByteCount (string s);
override this.GetByteCount : string -> int
Public Overrides Function GetByteCount (s As String) As Integer
参数
- s
- String
包含要编码的字符集的字符串。
返回
对指定字符进行编码后生成的字节数。
例外
s
上声明的默认值为 null
。
得到的字节数大于可作为整数返回的最大数量。
启用了错误检测,并且 s
包含无效的字符序列。
发生回退(有关详细信息,请参阅采用 .NET 的字符编码) -和- 将 EncoderFallback 设置为 EncoderExceptionFallback。
示例
下面的示例调用 GetMaxByteCount 和 GetByteCount(String) 方法来计算对字符串进行编码所需的最大字节数和实际字节数。 它还显示使用字节顺序标记存储字节流所需的实际字节数。
using System;
using System.Text;
class UTF8EncodingExample {
public static void Main() {
String chars = "UTF-16 Encoding Example";
Encoding unicode = Encoding.Unicode;
Console.WriteLine("Bytes needed to encode '{0}':", chars);
Console.WriteLine(" Maximum: {0}",
unicode.GetMaxByteCount(chars.Length));
Console.WriteLine(" Actual: {0}",
unicode.GetByteCount(chars));
Console.WriteLine(" Actual with BOM: {0}",
unicode.GetByteCount(chars) + unicode.GetPreamble().Length);
}
}
// The example displays the following output:
// Bytes needed to encode 'UTF-16 Encoding Example':
// Maximum: 48
// Actual: 46
// Actual with BOM: 48
Imports System.Text
Module Example
Public Sub Main()
Dim chars As String = "UTF-16 Encoding Example"
Dim unicode As Encoding = Encoding.Unicode
Console.WriteLine("Bytes needed to encode '{0}':", chars)
Console.WriteLine(" Maximum: {0}",
unicode.GetMaxByteCount(chars.Length))
Console.WriteLine(" Actual: {0}",
unicode.GetByteCount(chars))
Console.WriteLine(" Actual with BOM: {0}",
unicode.GetByteCount(chars) + unicode.GetPreamble().Length)
End Sub
End Module
' The example displays the following output:
' Bytes needed to encode 'UTF-16 Encoding Example':
' Maximum: 48
' Actual: 46
' Actual with BOM: 48
注解
若要计算存储生成的字节所需的 GetBytes 确切数组大小,请调用 GetByteCount 该方法。 若要计算最大数组大小,请调用 GetMaxByteCount 该方法。 该方法 GetByteCount 通常分配较少的内存,而 GetMaxByteCount 该方法通常执行速度更快。
通过错误检测,无效序列会导致此方法引发 ArgumentException。 如果没有错误检测,将忽略无效序列,并且不会引发异常。
重要
为了确保编码的字节在保存为文件或流时正确解码,可以将编码的字节流作为前言前缀。 在字节流开头插入谓词 (,例如要写入文件) 的一系列字节的开头是开发人员的责任,并且前言中的字节数不会反映在方法返回 GetByteCount(String) 的值中。
另请参阅
适用于
GetByteCount(Char*, Int32)
重要
此 API 不符合 CLS。
计算对从指定的字符指针开始的一组字符进行编码时产生的字节数。
public:
override int GetByteCount(char* chars, int count);
[System.CLSCompliant(false)]
public override int GetByteCount (char* chars, int count);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetByteCount (char* chars, int count);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount (char* chars, int count);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount (char* chars, int count);
[<System.CLSCompliant(false)>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetByteCount : nativeptr<char> * int -> int
参数
- chars
- Char*
指向第一个要编码的字符的指针。
- count
- Int32
要编码的字符的数目。
返回
对指定字符进行编码后生成的字节数。
- 属性
例外
chars
上声明的默认值为 null
。
启用了错误检测,检测到 chars
包含无效字符序列。
发生回退(有关详细信息,请参阅采用 .NET 的字符编码) -和- 将 EncoderFallback 设置为 EncoderExceptionFallback。
注解
若要计算存储生成的字节所需的确切数组大小 GetBytes ,请调用 GetByteCount 该方法。 若要计算最大数组大小,请调用 GetMaxByteCount 该方法。 该方法 GetByteCount 通常分配较少的内存,而 GetMaxByteCount 该方法通常执行速度更快。
通过错误检测,无效序列会导致此方法引发 ArgumentException。 如果没有错误检测,将忽略无效序列,并且不会引发异常。
重要
为了确保编码的字节在保存为文件或流时正确解码,可以将编码的字节流作为前言前缀。 在字节流开头插入谓词 (,例如要写入文件) 的一系列字节的开头是开发人员的责任,并且前言中的字节数不会反映在方法返回 GetByteCount 的值中。
另请参阅
适用于
GetByteCount(Char[], Int32, Int32)
计算对指定字符数组中的一组字符进行编码时产生的字节数。
public:
override int GetByteCount(cli::array <char> ^ chars, int index, int count);
public override int GetByteCount (char[] chars, int index, int count);
override this.GetByteCount : char[] * int * int -> int
Public Overrides Function GetByteCount (chars As Char(), index As Integer, count As Integer) As Integer
参数
- chars
- Char[]
包含要编码的字符集的字符数组。
- index
- Int32
第一个要编码的字符的索引。
- count
- Int32
要编码的字符的数目。
返回
对指定字符进行编码后生成的字节数。
例外
chars
为 null
(Nothing
)。
index
或 count
小于零。
或 -
index
和count
不表示chars
中的有效范围。或 - 得到的字节数大于可作为整数返回的最大数量。
启用了错误检测,并且 chars
包含无效的字符序列。
发生回退(有关详细信息,请参阅采用 .NET 的字符编码) -和- 将 EncoderFallback 设置为 EncoderExceptionFallback。
示例
以下示例使用拉丁文大写和小写字符填充数组,并调用 GetByteCount(Char[], Int32, Int32) 该方法来确定对拉丁文小写字符进行编码所需的字节数。 然后,它会显示此信息以及添加字节顺序标记时所需的字节总数。 它将此数字与方法返回 GetMaxByteCount 的值进行比较,该值指示对拉丁文小写字符进行编码所需的最大字节数。 以下示例使用希腊字符和西里尔文字符的组合填充数组,并调用 GetByteCount(Char[], Int32, Int32) 该方法来确定编码西里尔字符所需的字节数。 然后,它会显示此信息以及添加字节顺序标记时所需的字节总数。 它将此数字与方法返回 GetMaxByteCount 的值进行比较,该值指示编码西里尔文字符所需的最大字节数。
using System;
using System.Text;
public class Example
{
public static void Main()
{
int uppercaseStart = 0x0041;
int uppercaseEnd = 0x005a;
int lowercaseStart = 0x0061;
int lowercaseEnd = 0x007a;
// Instantiate a UTF8 encoding object with BOM support.
Encoding unicode = Encoding.Unicode;
// Populate array with characters.
char[] chars = new char[lowercaseEnd - lowercaseStart + uppercaseEnd - uppercaseStart + 2];
int index = 0;
for (int ctr = uppercaseStart; ctr <= uppercaseEnd; ctr++) {
chars[index] = (char)ctr;
index++;
}
for (int ctr = lowercaseStart; ctr <= lowercaseEnd; ctr++) {
chars[index] = (char)ctr;
index++;
}
// Display the bytes needed for the lowercase characters.
Console.WriteLine("Bytes needed for lowercase Latin characters:");
Console.WriteLine(" Maximum: {0,5:N0}",
unicode.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1));
Console.WriteLine(" Actual: {0,5:N0}",
unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
lowercaseEnd - lowercaseStart + 1));
Console.WriteLine(" Actual with BOM: {0,5:N0}",
unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
lowercaseEnd - lowercaseStart + 1) +
unicode.GetPreamble().Length);
}
}
// The example displays the following output:
// Bytes needed for lowercase Latin characters:
// Maximum: 54
// Actual: 52
// Actual with BOM: 54
Imports System.Text
Module Example
Public Sub Main()
Dim uppercaseStart As Integer = &h0041
Dim uppercaseEnd As Integer = &h005a
Dim lowercaseStart As Integer = &h0061
Dim lowercaseEnd As Integer = &h007a
' Instantiate a UTF8 encoding object with BOM support.
Dim unicode As Encoding = Encoding.Unicode
' Populate array with characters.
Dim chars(lowercaseEnd - lowercaseStart + uppercaseEnd - uppercaseStart + 1) As Char
Dim index As Integer = 0
For ctr As Integer = uppercaseStart To uppercaseEnd
chars(index) = ChrW(ctr)
index += 1
Next
For ctr As Integer = lowercaseStart To lowercaseEnd
chars(index) = ChrW(ctr)
index += 1
Next
' Display the bytes needed for the lowercase characters.
Console.WriteLine("Bytes needed for lowercase Latin characters:")
Console.WriteLine(" Maximum: {0,5:N0}",
unicode.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1))
Console.WriteLine(" Actual: {0,5:N0}",
unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
lowercaseEnd - lowercaseStart + 1))
Console.WriteLine(" Actual with BOM: {0,5:N0}",
unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
lowercaseEnd - lowercaseStart + 1) +
unicode.GetPreamble().Length)
End Sub
End Module
' The example displays the following output:
' Bytes needed for lowercase Latin characters:
' Maximum: 54
' Actual: 52
' Actual with BOM: 54
注解
若要计算存储生成的字节所需的 GetBytes 确切数组大小,应用程序使用 GetByteCount。 若要计算最大数组大小,请调用 GetMaxByteCount 该方法。 该方法 GetByteCount 通常分配较少的内存,而 GetMaxByteCount 该方法通常执行速度更快。
启用错误检测后,无效序列会导致此方法引发 ArgumentException。 如果没有错误检测,将忽略无效序列,并且不会引发异常。
为了确保编码的字节在保存为文件或流时正确解码,可以将编码的字节流作为前言前缀。 将谓词插入字节流开头 (,例如要写入文件) 的一系列字节的开头是开发人员的责任,前言中的字节数不反映在方法返回 GetByteCount(Char[], Int32, Int32) 的值中。