您的應用程式可以使用 GetEncoding 方法傳回指定編碼方式的編碼方式物件。應用程式可以使用 GetBytes 方法,將 Unicode 字串轉換成指定編碼方式中的位元組表示。
下列程式碼範例使用 GetEncoding 方法建立指定字碼頁的目標編碼方式物件。GetBytes 方法將在目標編碼方式物件上呼叫,將 Unicode 字串轉換成其在目標編碼方式中的位元組表示。然後顯示指定字碼頁的字串位元表示。
|
Imports System
Imports System.IO
Imports System.Globalization
Imports System.Text
Public Class Encoding_UnicodeToCP
Public Shared Sub Main()
' Converts ASCII characters to bytes.
' Displays the string's byte representation in the
' specified code page.
' Code page 1252 represents Latin characters.
PrintCPBytes("Hello, World!", 1252)
' Code page 932 represents Japanese characters.
PrintCPBytes("Hello, World!", 932)
' Converts Japanese characters.
PrintCPBytes("\u307b,\u308b,\u305a,\u3042,\u306d",1252)
PrintCPBytes("\u307b,\u308b,\u305a,\u3042,\u306d",932)
End Sub
Public Shared Sub PrintCPBytes(str As String, codePage As Integer)
Dim targetEncoding As Encoding
Dim encodedChars() As Byte
' Gets the encoding for the specified code page.
targetEncoding = Encoding.GetEncoding(codePage)
' Gets the byte representation of the specified string.
encodedChars = targetEncoding.GetBytes(str)
' Prints the bytes.
Console.WriteLine("Byte representation of '{0}' in CP '{1}':", _
str, codePage)
Dim i As Integer
For i = 0 To encodedChars.Length - 1
Console.WriteLine("Byte {0}: {1}", i, encodedChars(i))
Next i
End Sub
End Class
|
|
using System;
using System.IO;
using System.Globalization;
using System.Text;
public class Encoding_UnicodeToCP
{
public static void Main()
{
// Converts ASCII characters to bytes.
// Displays the string's byte representation in the
// specified code page.
// Code page 1252 represents Latin characters.
PrintCPBytes("Hello, World!",1252);
// Code page 932 represents Japanese characters.
PrintCPBytes("Hello, World!",932);
// Converts Japanese characters to bytes.
PrintCPBytes("\u307b,\u308b,\u305a,\u3042,\u306d",1252);
PrintCPBytes("\u307b,\u308b,\u305a,\u3042,\u306d",932);
}
public static void PrintCPBytes(string str, int codePage)
{
Encoding targetEncoding;
byte[] encodedChars;
// Gets the encoding for the specified code page.
targetEncoding = Encoding.GetEncoding(codePage);
// Gets the byte representation of the specified string.
encodedChars = targetEncoding.GetBytes(str);
// Prints the bytes.
Console.WriteLine
("Byte representation of '{0}' in Code Page '{1}':", str,
codePage);
for (int i = 0; i < encodedChars.Length; i++)
Console.WriteLine("Byte {0}: {1}", i, encodedChars[i]);
}
}
|
注意事項: |
|---|
| 如果您在主控台應用程式 (Console Application) 中使用這段程式碼,指定的 Unicode 文字項目可能無法正確顯示。主控台環境中的 Unicode 字元支援會根據執行的 Windows 作業系統版本而有所不同。 |
您可以在 ASP.NET 應用程式中使用這些方法,來決定回應字元所使用的編碼方式。應用程式應該將 ContentEncoding 屬性值設為適當方法所傳回的值。下列程式碼範例示範如何設定 HttpResponse.ContentEncoding。
|
' Explicitly sets ContentEncoding to UTF-8.
Response.ContentEncoding = Encoding.UTF8
' Sets ContentEncoding using the name of an encoding.
Response.ContentEncoding = Encoding.GetEncoding(name)
' Sets ContentEncoding using a code page number.
Response.ContentEncoding = Encoding.GetEncoding(codepageNumber)
|
|
// Explicitly sets the encoding to UTF-8.
Response.ContentEncoding = Encoding.UTF8;
// Sets ContentEncoding using the name of an encoding.
Response.ContentEncoding = Encoding.GetEncoding(name);
// Sets ContentEncoding using a code page number.
Response.ContentEncoding = Encoding.GetEncoding(codepageNumber);
|
對於多數的 ASP.NET 應用程式,如果要以使用者所預期的編碼方式顯示文字,您應該使 ContentEncoding 屬性與 ContentEncoding 屬性相符。
如需在 ASP.NET 中使用編碼方式的詳細資訊,請參閱通用工作快速入門中的多重編碼方式範例,以及 ASP.NET 快速入門中的設定文化特性和編碼方式範例。