在 C# 中,内存中的所有字符串都是按 Unicode (UTF-16) 编码的。将数据从存储器移动到 string 对象中后,数据将自动转换为 UTF-16。如果数据仅包含从 0 到 127 的 ASCII 值,则此转换无需您执行任何额外的工作。但若源文本包含扩展的 ASCII 字节值(128 到 255),则默认情况下,将根据当前代码页解释扩展字符。若要指定应该根据其他某个代码页解释源文本,请使用 System.Text..::.Encoding 类,如下面的示例所示。
下面的示例演示如何转换按 8 位 ASCII 编码的文本文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
//yngqq@2024年9月3日15:22:45
namespace ConsoleApp1
{
class ANSIToUnicode
{
static void Main()
{
// Create a file that contains the Greek work ψυχή (psyche) when interpreted by using
// code page 737 ((DOS) Greek). You can also create the file by using Character Map
/