Encoding.GetPreamble 方法
在派生类中重写时,返回指定所用编码的字节序列。
**命名空间:**System.Text
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Overridable Function GetPreamble As Byte()
用法
Dim instance As Encoding
Dim returnValue As Byte()
returnValue = instance.GetPreamble
public virtual byte[] GetPreamble ()
public:
virtual array<unsigned char>^ GetPreamble ()
public byte[] GetPreamble ()
public function GetPreamble () : byte[]
返回值
一个字节数组,包含指定所用编码的字节序列。 - 或 - 长度为零的字节数组(如果不需要前导码)。
备注
UTF-16 和 UTF-32 编码器可以使用 Big-Endian 字节顺序(从最高有效字节开始),也可以使用 Little-Endian 字节顺序(从最低有效字节开始)。例如,大写拉丁字母 A (U+0041) 的序列化结果(十六进制)如下所示:
UTF-16 Big-Endian 字节顺序:00 41
UTF-16 Little-Endian 字节顺序:41 00
UTF-32 Big-Endian 字节顺序:00 00 00 41
UTF-32 Little-Endian 字节顺序:41 00 00 00
或者,Encoding 提供一个前导码(即一个字节数组),可以将它作为编码过程中所产生的字节序列的前缀。如果前导码中包含字节顺序标记(在 Unicode 中,码位为 U+FEFF),则它会帮助解码器确定字节顺序和转换格式或 UTF。Unicode 字节顺序标记的序列化结果(十六进制)如下所示:
UTF-8:EF BB BF
UTF-16 Big-Endian 字节顺序:FE FF
UTF-16 Little-Endian 字节顺序:FF FE
UTF-32 Big-Endian 字节顺序:00 00 FE FF
UTF-32 Little-Endian 字节顺序:FF FE 00 00
通常,使用本机字节顺序存储 Unicode 字符的效率更高。例如,在 Little-Endian 平台(如 Intel 计算机)上最好使用 Little-Endian 字节顺序。
有关字节顺序和字节顺序标记的更多信息,请参见 www.unicode.org 上的“The Unicode Standard”(Unicode 标准)部分。
警告
若要确保编码后的字节能够正确解码,请在编码后的字节前添加前导码作为前缀。但是,大多数编码不提供前导码。若要确保编码后的字节能够正确解码,请使用带前导码的 Unicode 编码(即 UTF8Encoding、UnicodeEncoding 或 UTF32Encoding)
示例
下面的代码示例确定基于前导码的编码字节顺序。
Imports System
Imports System.Text
Namespace GetPreambleExample
Class GetPreambleExampleClass
Shared Sub Main()
Dim [unicode] As Encoding = Encoding.Unicode
' Get the preamble for the Unicode encoder.
' In this case the preamble contains the byte order mark (BOM).
Dim preamble As Byte() = [unicode].GetPreamble()
' Make sure a preamble was returned
' and is large enough to contain a BOM.
If preamble.Length >= 2 Then
If preamble(0) = &HFE And preamble(1) = &HFF Then
Console.WriteLine("The Unicode encoder is encoding in big-endian order.")
Else
If preamble(0) = &HFF And preamble(1) = &HFE Then
Console.WriteLine("The Unicode encoder is encoding in little-endian order.")
End If
End If
End If
End Sub
End Class
End Namespace
using System;
using System.Text;
namespace GetPreambleExample
{
class GetPreambleExampleClass
{
static void Main()
{
Encoding unicode = Encoding.Unicode;
// Get the preamble for the Unicode encoder.
// In this case the preamble contains the byte order mark (BOM).
byte[] preamble = unicode.GetPreamble();
// Make sure a preamble was returned
// and is large enough to containa BOM.
if(preamble.Length >= 2)
{
if(preamble[0] == 0xFE && preamble[1] == 0xFF)
{
Console.WriteLine("The Unicode encoder is encoding in big-endian order.");
}
else if(preamble[0] == 0xFF && preamble[1] == 0xFE)
{
Console.WriteLine("The Unicode encoder is encoding in little-endian order.");
}
}
}
}
}
using namespace System;
using namespace System::Text;
int main()
{
Encoding^ unicode = Encoding::Unicode;
// Get the preamble for the Unicode encoder.
// In this case the preamblecontains the Byte order mark (BOM).
array<Byte>^preamble = unicode->GetPreamble();
// Make sure a preamble was returned
// and is large enough to containa BOM.
if ( preamble->Length >= 2 )
{
// if (preamble->Item[0] == 0xFE && preamble->Item[1] == 0xFF)
if ( preamble[ 0 ] == 0xFE && preamble[ 1 ] == 0xFF )
{
Console::WriteLine( "The Unicode encoder is encoding in big-endian order." );
}
// else if (preamble->Item[0] == 0xFF && preamble->Item[1] == 0xFE)
else
// else if (preamble->Item[0] == 0xFF && preamble->Item[1] == 0xFE)
if ( preamble[ 0 ] == 0xFF && preamble[ 1 ] == 0xFE )
{
Console::WriteLine( "The Unicode encoder is encoding in little-endian order." );
}
}
}
package GetPreambleExample;
import System.*;
import System.Text.*;
class GetPreambleExampleClass
{
public static void main(String[] args)
{
Encoding unicode = Encoding.get_Unicode();
// Get the preamble for the Unicode encoder.
// In this case the preamble contains the byte order mark (BOM).
ubyte preamble[] = unicode.GetPreamble();
// Make sure a preamble was returned
// and is large enough to containa BOM.
if(preamble.length >= 2) {
if(preamble[0] == 0xFE && preamble[1] == 0xFF) {
Console.WriteLine("The Unicode encoder is "
+ "encoding in big-endian order.");
}
else {
if(preamble[0] == 0xFF && preamble[1] == 0xFE) {
Console.WriteLine("The Unicode encoder is"
+ " encoding in little-endian order.");
}
}
}
} //main
} //GetPreambleExampleClass
平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0