decoder

decoder,計算機語言函式,意思是將編碼位元組序列轉換為一組字元。

語法

C#

[SerializableAttribute]

[ComVisibleAttribute(true)]

public abstract class Decoder

C++

[SerializableAttribute]

[ComVisibleAttribute(true)]

public ref class Decoder abstract

VB

聲明

<SerializableAttribute> _

<ComVisibleAttribute(True)> _

Public MustInherit Class Decoder

用法

Dim instance As Decoder

備註

編碼是將一組字元轉換為一個位元組序列的過程。解碼是一個反向操作過程,即將一個編碼位元組序列轉換為一組字元。

Decoder 在連續調用 GetChars 之間維護狀態信息,因此它可以正確地對跨塊的位元組序列進行解碼。 Decoder 還保留數據塊結尾的尾部位元組並將這些尾部位元組用在下一次解碼操作中。因此,GetDecoder 和 GetEncoder 在網路傳輸和檔案操作中很有用,這是因為這些操作通常處理數據塊而不是完整的數據流。

GetCharCount 方法確定有多少字元導致對位元組序列進行解碼,而 GetChars 方法執行實際的解碼。

若要獲得此類某個實現的實例,請使用 Encoding 實現的 GetDecoder 方法。

版本注意事項

轉換操作期間可以序列化 Decoder 或 Encoder 對象。如果在相同版本的 .NET Framework 中反序列化對象,則保留對象的狀態,但如果在另一個版本中反序列化對象,則對象的狀態會丟失。

給繼承者的說明 從此類繼承時,您必須重寫所有成員。

示例

c#

using System;

using System.Text;

public class dec

{

public static void Main()

{

// These bytes in UTF-8 correspond to 3 different Unicode

// characters: space (U+0020), # (U+0023), and the biohazard

// symbol (U+2623). Note the biohazard symbol requires 3 bytes

// in UTF-8 (hexadecimal e2, 98, a3). Decoders store state across

// multiple calls to GetChars, handling the case when one char

// is in multiple byte arrays.

byte[] bytes1 = { 0x20, 0x23, 0xe2 };

byte[] bytes2 = { 0x98, 0xa3 };

char[] chars = new char[3];

Decoder d = Encoding.UTF8.GetDecoder();

int charLen = d.GetChars(bytes1, 0, bytes1.Length, chars, 0);

// The value of charLen should be 2 now.

charLen += d.GetChars(bytes2, 0, bytes2.Length, chars, charLen);

foreach(char c in chars)

Console.Write("U+{0:X4} ", (ushort)c);

}

}

C++

using namespace System;

using namespace System::Text;

int main()

{

// These bytes in UTF-8 correspond to 3 different Unicode

// characters: space (U+0020), # (U+0023), and the biohazard

// symbol (U+2623). Note the biohazard symbol requires 3 bytes

// in UTF-8 (hexadecimal e2, 98, a3). Decoders store state across

// multiple calls to GetChars, handling the case when one char

// is in multiple byte arrays. array<Byte>^bytes1 = {0x20,0x23,0xe2};

array<Byte>^bytes2 = {0x98,0xa3};

array<Char>^chars = gcnew array<Char>(3);

Decoder^ d = Encoding::UTF8->GetDecoder();

int charLen = d->GetChars( bytes1, 0, bytes1->Length, chars, 0 );

// The value of charLen should be 2 now.

charLen += d->GetChars( bytes2, 0, bytes2->Length, chars, charLen );

for ( UInt16 index(0); index < chars->Length; ++index )

{

Console::Write( "U+{0:X4} ", static_cast<UInt16>(chars[ index ]) );

}

}

相關詞條

熱門詞條

聯絡我們