使用 Unicode 编码
面向公共语言运行时的应用程序使用编码将字符表示形式从本机字符方案(Unicode)映射为其他方案。应用程序使用解码将字符从非本机方案(非 Unicode)映射为本机方案。System.Text 命名空间提供了很多类,应用程序可使用这些类对字符进行编码和解码操作。了解编码对这些类进行了介绍。
Unicode 转换格式
Unicode 标准为所支持的每个脚本中的每个字符分配一个码位(一个数字)。Unicode 转换格式 (UTF) 是一种码位编码格式。Unicode 标准 3.2 版使用下表中定义的 UTF 和其他编码。对于所有编码,内部 .NET Framework 字符串都是本机 UTF-16 字符串。有关更多信息,请参见位于 Unicode home page(Unicode 主页)的“Unicode Standard”(Unicode 标准)。
Unicode UTF-32 编码
将 Unicode 字符表示为 32 位整数序列。应用程序可以使用 UTF32Encoding 类在字符和 UTF-32 编码之间相互转换。如果编码空间对于操作系统非常重要,应用程序又需要避免 UTF-16 在操作系统上的代理项码位行为,则可以使用 UTF-32。请注意,显示器上呈现的单个“符号”仍可以使用多个 UTF-32 字符来编码。目前,易受此行为影响的辅助字符比 Unicode BMP 字符少得多。
Unicode UTF-16 编码
将 Unicode 字符表示为 16 位整数序列。应用程序可以使用 UnicodeEncoding 类在字符和 UTF-16 编码之间相互转换。UTF-16 通常以本机方式使用,就像在 Microsoft.Net char 类型、Windows WCHAR 类型和其他常见类型中一样。大多数常见的 Unicode 码位都只接受一个 UTF-16 码位(2 个字节)。Unicode 辅助字符 U+10000 及以上仍然需要两个 UTF-16 代理项码位。
Unicode UTF-8 编码
将 Unicode 字符表示为 8 位字节序列。应用程序可以使用 UTF8Encoding 类在字符和 UTF-8 编码之间相互转换。UTF-8 允许使用 8 位数据大小进行编码,可用在许多现有操作系统上。对于 ASCII 范围的字符,UTF-8 与 ASCII 编码相同,并且允许更宽的字符集。但是,对于 CJK 语言,UTF-8 可能要求每个字符使用三个字节,这样,数据大小可能超过 UTF-16。请注意,有时 ASCII 数据(如 HTML 标记)的数量正是 CJK 范围增大的数量。
Unicode UTF-7 编码
将 Unicode 字符表示为 7 位 ASCII 字符序列。应用程序可以使用 UTF7Encoding 类在字符和 UTF-7 编码之间相互转换。非 ASCII Unicode 字符由 ASCII 字符的转义序列表示。UTF-7 支持某些协议,最常见的是电子邮件和新闻组协议。但是,UTF-7 并非特别安全或可靠。在某些情况下,更改一个位可能会完全改变对整个 UTF-7 字符串的解释。在另一些情况下,不同的 UTF-7 字符串可能编码成相同的文本。对于包含非 ASCII 字符的序列,UTF-7 的空间利用率大大低于 UTF-8,编码/解码的速度也更慢。因此,应用程序通常应优先考虑 UTF-8 而不是 UTF-7。
ASCII 编码
将拉丁字母编码为单个 7 位 ASCII 字符。由于此编码仅支持从 U+0000 到 U+007F 的字符值,因此,大多数情况下,对于国际化应用程序来说,它是不够用的。应用程序可以使用 ASCIIEncoding 类在字符和 ASCII 编码之间相互转换。有关在代码中使用此类的示例,请参见为基类型编码。ANSI/ISO 编码
用于非 Unicode 编码。Encoding 类提供对范围较广的 ANSI/ISO 编码的支持。
以字符串格式传递二进制数据
随机数字集合(无论是字节还是字符)无法生成有效的字符串或有效的 Unicode。应用程序无法在字节数组和 Unicode 之间相互转换。在 Unicode 5.0 中,某些字符和码位序列是非法的,因此,不要使用任何 Unicode 编码进行转换。如果应用程序必须以字符串格式传递二进制数据,则应使用专为此设计的 Base 64 或其他格式。
使用 Encoding 类
应用程序可以使用 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]);
}
}
备注
如果在控制台应用程序中使用此代码,则指定的 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 快速入门中的“设置区域性和编码示例”。