UTF8Encoding.GetPreamble 方法

如果此实例的构造函数请求提供一个字节顺序标记,则返回用 UTF-8 格式编码的 Unicode 字节顺序标记。

**命名空间:**System.Text
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overrides Function GetPreamble As Byte()
用法
Dim instance As UTF8Encoding
Dim returnValue As Byte()

returnValue = instance.GetPreamble
public override byte[] GetPreamble ()
public:
virtual array<unsigned char>^ GetPreamble () override
public byte[] GetPreamble ()
public override function GetPreamble () : byte[]

返回值

如果此实例的构造函数请求提供字节顺序标记,则返回一个包含 Unicode 字节顺序标记的字节数组。 否则,返回一个长度为零的字节数组。

备注

或者,UTF8Encoding 提供一个前导码(即一个字节数组),可以将它作为编码过程中产生的字节序列的前缀。如果前导码中包含字节顺序标记(码位为 U+FEFF),则它会帮助解码器确定字节顺序和转换格式或 UTF。Unicode 字节顺序标记被序列化为 EF BB BF(十六进制)。GetPreamble 方法返回一个包含字节顺序标记的字节数组。

有关 Unicode 编码、字节顺序和字节顺序标记的更多信息,请参见 www.unicode.org 上的“The Unicode Standard”(Unicode 标准)部分。

警告

若要确保编码后的字节能够正确解码,请在编码后的字节前添加前导码作为前缀。

示例

下面的示例演示了如何使用 GetPreamble 方法返回以 UTF-8 格式编码的 Unicode 字节顺序标记。请注意,UTF8Encoding 的默认构造函数不提供前导码。

Imports System
Imports System.Text

Class UTF8EncodingExample
    
    Public Shared Sub Main()
        ' The default constructor does not provide a preamble.
        Dim UTF8NoPreamble As New UTF8Encoding()
        Dim UTF8WithPreamble As New UTF8Encoding(True)
        
        Dim preamble() As Byte
        
        preamble = UTF8NoPreamble.GetPreamble()
        Console.WriteLine("UTF8NoPreamble")
        Console.WriteLine(" preamble length: {0}", preamble.Length)
        Console.Write(" preamble: ")
        ShowArray(preamble)
        
        preamble = UTF8WithPreamble.GetPreamble()
        Console.WriteLine("UTF8WithPreamble")
        Console.WriteLine(" preamble length: {0}", preamble.Length)
        Console.Write(" preamble: ")
        ShowArray(preamble)
    End Sub 'Main
    
    
    Public Shared Sub ShowArray(theArray As Array)
        Dim o As Object
        For Each o In  theArray
            Console.Write("[{0}]", o)
        Next o
        Console.WriteLine()
    End Sub 'ShowArray
End Class 'UTF8EncodingExample
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        // The default constructor does not provide a preamble.
        UTF8Encoding UTF8NoPreamble = new UTF8Encoding();
        UTF8Encoding UTF8WithPreamble = new UTF8Encoding(true);

        Byte[] preamble;

        preamble = UTF8NoPreamble.GetPreamble();
        Console.WriteLine("UTF8NoPreamble");
        Console.WriteLine(" preamble length: {0}", preamble.Length);
        Console.Write(" preamble: ");
        ShowArray(preamble);

        preamble = UTF8WithPreamble.GetPreamble();
        Console.WriteLine("UTF8WithPreamble");
        Console.WriteLine(" preamble length: {0}", preamble.Length);
        Console.Write(" preamble: ");
        ShowArray(preamble);
    }

    public static void ShowArray(Array theArray) {
        foreach (Object o in theArray) {
            Console.Write("[{0}]", o);
        }
        Console.WriteLine();
    }
}
using namespace System;
using namespace System::Text;
using namespace System::Collections;
void ShowArray( Array^ theArray )
{
   IEnumerator^ myEnum = theArray->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ o = safe_cast<Object^>(myEnum->Current);
      Console::Write( "[{0}]", o );
   }

   Console::WriteLine();
}

int main()
{
   
   // The default constructor does not provide a preamble.
   UTF8Encoding^ UTF8NoPreamble = gcnew UTF8Encoding;
   UTF8Encoding^ UTF8WithPreamble = gcnew UTF8Encoding( true );
   array<Byte>^preamble;
   preamble = UTF8NoPreamble->GetPreamble();
   Console::WriteLine( "UTF8NoPreamble" );
   Console::WriteLine( " preamble length: {0}", preamble->Length );
   Console::Write( " preamble: " );
   ShowArray( preamble );
   preamble = UTF8WithPreamble->GetPreamble();
   Console::WriteLine( "UTF8WithPreamble" );
   Console::WriteLine( " preamble length: {0}", preamble->Length );
   Console::Write( " preamble: " );
   ShowArray( preamble );
}
import System.*;
import System.Text.*;

class UTF8EncodingExample
{
    public static void main(String[] args)
    {
        // The default constructor does not provide a preamble.
        UTF8Encoding utf8NoPreamble = new UTF8Encoding();
        UTF8Encoding utf8WithPreamble = new UTF8Encoding(true);
        ubyte preamble[];

        preamble = utf8NoPreamble.GetPreamble();
        Console.WriteLine("UTF8NoPreamble");
        Console.WriteLine(" preamble length: {0}",
            String.valueOf(preamble.length));
        Console.Write(" preamble: ");
        ShowArray(preamble);
        preamble = utf8WithPreamble.GetPreamble();
        Console.WriteLine("UTF8WithPreamble");
        Console.WriteLine(" preamble length: {0}",
            String.valueOf(preamble.length));
        Console.Write(" preamble: ");
        ShowArray(preamble);
    } //main

    public static void ShowArray(Array theArray)
    {
        Object o = null;
        for (int iCtr = 0; iCtr < theArray.get_Count(); iCtr++) {
            o = theArray.get_Item(iCtr);
            Console.Write("[{0}]", o);
        }
        Console.WriteLine();
    } //ShowArray
} //UTF8EncodingExample

平台

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

请参见

参考

UTF8Encoding 类
UTF8Encoding 成员
System.Text 命名空间