UTF32Encoding.GetString(Byte[], Int32, Int32) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將位元組陣列中的位元組範圍解碼成字串。
public:
override System::String ^ GetString(cli::array <System::Byte> ^ bytes, int index, int count);
public override string GetString(byte[] bytes, int index, int count);
override this.GetString : byte[] * int * int -> string
Public Overrides Function GetString (bytes As Byte(), index As Integer, count As Integer) As String
參數
- bytes
- Byte[]
包含要解碼的位元組序列的位元組陣列。
- index
- Int32
第一個要解碼的位元組的索引。
- count
- Int32
需要解碼的位元組數。
傳回
包含解碼指定位元組序列結果的字串。
例外狀況
bytes 是 null。
錯誤偵測已啟用,且 bytes 包含無效的位元組序列。
因此出現了備用機制(更多資訊請參見字元編碼在.NET中的完整說明)。
-及-
範例
以下範例將字串編碼成兩個位元組陣列,一個為小端序,另一個為大端序。 接著它會將位元組解碼回字串。
using System;
using System.Text;
public class Example
{
public static void Main()
{
// Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
UTF32Encoding u32LE = new UTF32Encoding(false, true, true);
UTF32Encoding u32BE = new UTF32Encoding(true, true, true);
// Create byte arrays from the same string containing the following characters:
// Latin Small Letter Z (U+007A)
// Latin Small Letter A (U+0061)
// Combining Breve (U+0306)
// Latin Small Letter AE With Acute (U+01FD)
// Greek Small Letter Beta (U+03B2)
String str = "za\u0306\u01FD\u03B2";
// barrBE uses the big-endian byte order.
byte[] barrBE = new byte[u32BE.GetByteCount(str)];
u32BE.GetBytes(str, 0, str.Length, barrBE, 0);
// barrLE uses the little-endian byte order.
byte[] barrLE = new byte[u32LE.GetByteCount(str)];
u32LE.GetBytes(str, 0, str.Length, barrLE, 0);
// Decode the byte arrays.
Console.WriteLine("BE array with BE encoding:");
DisplayString(barrBE, u32BE);
Console.WriteLine();
Console.WriteLine("LE array with LE encoding:");
DisplayString(barrLE, u32LE);
Console.WriteLine();
// Decode the byte arrays using an encoding with a different byte order.
Console.WriteLine("BE array with LE encoding:");
try {
DisplayString(barrBE, u32LE);
}
catch (System.ArgumentException e) {
Console.WriteLine(e.Message);
}
Console.WriteLine();
Console.WriteLine("LE array with BE encoding:");
try {
DisplayString(barrLE, u32BE);
}
catch (ArgumentException e) {
Console.WriteLine(e.Message);
}
Console.WriteLine();
}
public static void DisplayString(byte[] bytes, Encoding enc)
{
// Display the name of the encoding used.
Console.Write("{0,-25}: ", enc.ToString());
// Decode the bytes and display the characters.
Console.WriteLine(enc.GetString(bytes, 0, bytes.Length));
}
}
// This example displays the following output:
// BE array with BE encoding:
// System.Text.UTF32Encoding: zăǽβ
//
// LE array with LE encoding:
// System.Text.UTF32Encoding: zăǽβ
//
// BE array with LE encoding:
// System.Text.UTF32Encoding: Unable to translate bytes [00][00][00][7A] at index 0 from specified code page to Unicode.
//
// LE array with BE encoding:
// System.Text.UTF32Encoding: Unable to translate bytes [7A][00][00][00] at index 0 from specified code page to Unicode.
Imports System.Text
Public Module Example
Public Sub Main()
' Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
Dim u32LE As New UTF32Encoding(False, True, True)
Dim u32BE As New UTF32Encoding(True, True, True)
' Create byte arrays from the same string containing the following characters:
' Latin Small Letter Z (U+007A)
' Latin Small Letter A (U+0061)
' Combining Breve (U+0306)
' Latin Small Letter AE With Acute (U+01FD)
' Greek Small Letter Beta (U+03B2)
Dim str As String = "za" + ChrW(&h0306) + ChrW(&h01FD) + ChrW(&h03B2)
' barrBE uses the big-endian byte order.
Dim barrBE(u32BE.GetByteCount(str) - 1) As Byte
u32BE.GetBytes(str, 0, str.Length, barrBE, 0)
' barrLE uses the little-endian byte order.
Dim barrLE(u32LE.GetByteCount(str) - 1) As Byte
u32LE.GetBytes(str, 0, str.Length, barrLE, 0)
' Decode the byte arrays.
Console.WriteLine("BE array with BE encoding:")
DisplayString(barrBE, u32BE)
Console.WriteLine()
Console.WriteLine("LE array with LE encoding:")
DisplayString(barrLE, u32LE)
Console.WriteLine()
' Decode the byte arrays using an encoding with a different byte order.
Console.WriteLine("BE array with LE encoding:")
Try
DisplayString(barrBE, u32LE)
Catch e As ArgumentException
Console.WriteLine(e.Message)
End Try
Console.WriteLine()
Console.WriteLine("LE array with BE encoding:")
Try
DisplayString(barrLE, u32BE)
Catch e As ArgumentException
Console.WriteLine(e.Message)
End Try
Console.WriteLine()
End Sub
Public Sub DisplayString(bytes As Byte(), enc As Encoding )
' Display the name of the encoding used.
Console.Write("{0,-25}: ", enc.ToString())
' Decode the bytes and display the characters.
Console.WriteLine(enc.GetString(bytes, 0, bytes.Length))
End Sub
End Module
' This example displays the following output:
' BE array with BE encoding:
' System.Text.UTF32Encoding: zăǽβ
'
' LE array with LE encoding:
' System.Text.UTF32Encoding: zăǽβ
'
' BE array with LE encoding:
' System.Text.UTF32Encoding: Unable to translate bytes [00][00][00][7A] at index 0 from specified code page to Unicode.
'
' LE array with BE encoding:
' System.Text.UTF32Encoding: Unable to translate bytes [7A][00][00][00] at index 0 from specified code page to Unicode.
以下範例透過呼叫 GetByteCount 該方法來初始化陣列,以確定編碼字串所需的位元組數量,然後加上位元組順序標記(BOM)的大小。 範例接著呼叫 GetPreamble 將BOM儲存到陣列的方法,再呼叫 GetBytes 將編碼的位元組儲存到陣列的方法。 範例接著呼叫 GetString 該方法來解碼該字串。
using System;
using System.Text;
public class Example
{
public static void Main()
{
var utf32 = new UTF32Encoding(! BitConverter.IsLittleEndian, true);
String s = "It was the best of times, it was the worst of times...";
// We need to dimension the array, since we'll populate it with 2 method calls.
Byte[] bytes = new Byte[utf32.GetByteCount(s) + utf32.GetPreamble().Length];
// Encode the string.
Array.Copy(utf32.GetPreamble(), bytes, utf32.GetPreamble().Length);
utf32.GetBytes(s, 0, s.Length, bytes, utf32.GetPreamble().Length);
// Decode the byte array.
String s2 = utf32.GetString(bytes, 0, bytes.Length);
Console.WriteLine(s2);
}
}
// The example displays the following output:
// ?It was the best of times, it was the worst of times...
Imports System.Text
Module Example
Public Sub Main()
Dim utf32 As New UTF32Encoding(Not BitConverter.IsLittleEndian, True)
Dim s As String = "It was the best of times, it was the worst of times..."
' We need to dimension the array, since we'll populate it with 2 method calls.
Dim bytes(utf32.GetByteCount(s) + utf32.GetPreamble().Length - 1) As Byte
' Encode the string.
Array.Copy(utf32.GetPreamble(), bytes, utf32.GetPreamble().Length)
utf32.GetBytes(s, 0, s.Length, bytes, utf32.GetPreamble().Length)
' Decode the byte array.
Dim s2 As String = utf32.GetString(bytes, 0, bytes.Length)
Console.WriteLine(s2)
End Sub
End Module
' The example displays the following output:
' ?It was the best of times, it was the worst of times...
請注意,此處解碼後的字串與原始字串不同,因為它以 32 位元元組序標記 U+FFFE U+0000 開頭。 這表示兩個字串的比較結果會不相等,若輸出字串,BOM 會以替換字元「?」顯示。
備註
在錯誤檢測中,若序列無效,該方法會拋出 ArgumentException。 若無錯誤偵測,無效序列會被忽略,且不會拋出例外。
若解碼的位元組範圍包含位元組順序標記(BOM),且位元組陣列由非 BOM 識別型態的方法回傳,則字元 U+FFFE 會包含在該方法回傳的字元陣列中。 你可以透過呼叫 String.TrimStart 該方法來移除它。
待轉換的資料,例如從串流讀取的資料,可能只能以連續區塊形式取得。 在這種情況下,或資料量龐大到需要分割成較小區塊時,應用程式應分別使用DecoderEncoder方法或GetEncoder方法所提供的GetDecoder。