Encoding.GetPreamble メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
派生クラスでオーバーライドされると、使用されるエンコードを指定するバイト シーケンスを返します。
public:
virtual cli::array <System::Byte> ^ GetPreamble();
public virtual byte[] GetPreamble();
abstract member GetPreamble : unit -> byte[]
override this.GetPreamble : unit -> byte[]
Public Overridable Function GetPreamble () As Byte()
返品
使用されるエンコードを指定するバイトシーケンスを含むバイト配列。
-又は-
プリアンブルが必要ない場合は、長さ 0 のバイト配列。
例
次の例では、プリアンブルに基づいてエンコードのバイト順を決定します。
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 contain a 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.");
}
}
}
}
}
/*
This code produces the following output.
The Unicode encoder is encoding in little-endian order.
*/
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
'This code produces the following output.
'
'The Unicode encoder is encoding in little-endian order.
'
注釈
必要に応じて、 Encoding オブジェクトはプリアンブルを提供します。プリアンブルは、エンコード プロセスに起因するバイト シーケンスの前に付けることができるバイトの配列です。 プリアンブルにバイトオーダーマーク(Unicode、コードポイントU+FEFF)が含まれている場合、デコーダーがバイトオーダーと変換形式またはUTFを決定するのに役立ちます。
Unicode バイトオーダー マーク (BOM) は、次のようにシリアル化されます (16 進数)。
UTF-8: EF BB BF
UTF-16 ビッグ エンディアンバイトオーダー: FE FF
UTF-16 リトル エンディアン バイト順: FF FE
UTF-32 ビッグ エンディアン バイトオーダー: 00 00 FE FF
UTF-32 リトル エンディアン バイト順: FF FE 00 00
BOM を使用する必要があります。これは、タグなしまたは不適切にタグ付けされた Web データや、企業が国際的な懸念やその他のデータを持っていないときに格納されたランダムテキスト ファイルなど、 Encoding オブジェクトへの参照を失ったファイルのエンコードのほぼ一定の識別を提供するためです。 多くの場合、UTF-8 または UTF-16 でデータが一貫して適切にタグ付けされている場合は、ユーザーの問題を回避できます。
エンコードの種類を提供する標準の場合、BOM はやや冗長です。 ただし、サーバーが正しいエンコード ヘッダーを送信するのに役立ちます。 または、エンコードが失われた場合のフォールバックとして使用することもできます。
BOM を使用するには、いくつかの欠点があります。 たとえば、BOM を使用するデータベース フィールドを制限する方法を知ることは困難な場合があります。 ファイルの連結は、たとえば、不要な文字がデータの途中に入り込むような方法でファイルをマージする場合にも問題になる可能性があります。 ただし、いくつかの欠点があるにもかかわらず、BOM の使用を強くお勧めします。
バイト順とバイト順マークの詳細については、 Unicode ホーム ページの Unicode 標準を参照してください。
Caution
エンコードされたバイトが正しくデコードされるようにするには、エンコードされたバイトにプリアンブルのプレフィックスを付ける必要があります。 ただし、ほとんどのエンコードではプリアンブルは提供されません。 エンコードされたバイトが正しくデコードされるようにするには、Unicode エンコード ( UTF8Encoding、 UnicodeEncoding、または UTF32Encoding) をプリアンブルで使用する必要があります。