从 .NET Framework 4.5 开始,Console 类支持与 UnicodeEncoding 类的 UTF-16 编码。  显示 Unicode 字符到控制台,你可以设置 OutputEncoding 属性为 UTF8EncodingUnicodeEncoding

下面的示例显示 Unicode 字符的范围到控制台中。  该示例接受三个命令行参数:显示范围的开头,显示范围的末尾,以及是否使用当前控制台编码 (false) 或 UTF-16 编码 (true)。  假定控制台使用一个 TrueType 字体。

    class Program
{
private static void Main(string[] args)
{
uint rangeStart = ;
uint rangeEnd = ;
bool setOutputEncodingToUnicode = true;
// Get the current encoding so we can restore it.
Encoding originalOutputEncoding = Console.OutputEncoding; try
{
switch (args.Length)
{
case :
rangeStart = uint.Parse(args[], NumberStyles.HexNumber);
rangeEnd = uint.Parse(args[], NumberStyles.HexNumber);
setOutputEncodingToUnicode = true;
break;
case :
if (!uint.TryParse(args[], NumberStyles.HexNumber, null, out rangeStart))
throw new ArgumentException(String.Format("{0} is not a valid hexadecimal number.", args[])); if (!uint.TryParse(args[], NumberStyles.HexNumber, null, out rangeEnd))
throw new ArgumentException(String.Format("{0} is not a valid hexadecimal number.", args[])); bool.TryParse(args[], out setOutputEncodingToUnicode);
break;
default:
Console.WriteLine("Usage: {0} <{1}> <{2}> [{3}]",
Environment.GetCommandLineArgs()[],
"startingCodePointInHex",
"endingCodePointInHex",
"<setOutputEncodingToUnicode?{true|false, default:false}>");
return;
} if (setOutputEncodingToUnicode)
{
// This won't work before .NET Framework 4.5.
try
{
// Set encoding using endianness of this system.
// We're interested in displaying individual Char objects, so
// we don't want a Unicode BOM or exceptions to be thrown on
// invalid Char values.
Console.OutputEncoding = new UnicodeEncoding(!BitConverter.IsLittleEndian, false);
Console.WriteLine("\nOutput encoding set to UTF-16");
}
catch (IOException)
{
Console.OutputEncoding = new UTF8Encoding();
Console.WriteLine("Output encoding set to UTF-8");
}
}
else
{
Console.WriteLine("The console encoding is {0} (code page {1})",
Console.OutputEncoding.EncodingName,
Console.OutputEncoding.CodePage);
}
DisplayRange(rangeStart, rangeEnd);
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// Restore console environment.
Console.OutputEncoding = originalOutputEncoding;
}
} public static void DisplayRange(uint start, uint end)
{
const uint upperRange = 0x10FFFF;
const uint surrogateStart = 0xD800;
const uint surrogateEnd = 0xDFFF; if (end <= start)
{
uint t = start;
start = end;
end = t;
} // Check whether the start or end range is outside of last plane.
if (start > upperRange)
throw new ArgumentException(String.Format("0x{0:X5} is outside the upper range of Unicode code points (0x{1:X5})",
start, upperRange));
if (end > upperRange)
throw new ArgumentException(String.Format("0x{0:X5} is outside the upper range of Unicode code points (0x{0:X5})",
end, upperRange)); // Since we're using 21-bit code points, we can't use U+D800 to U+DFFF.
if ((start < surrogateStart & end > surrogateStart) || (start >= surrogateStart & start <= surrogateEnd))
throw new ArgumentException(String.Format("0x{0:X5}-0x{1:X5} includes the surrogate pair range 0x{2:X5}-0x{3:X5}",
start, end, surrogateStart, surrogateEnd));
uint last = RoundUpToMultipleOf(0x10, end);
uint first = RoundDownToMultipleOf(0x10, start); uint rows = (last - first) / 0x10; for (uint r = ; r < rows; ++r)
{
// Display the row header.
Console.Write("{0:x5} ", first + 0x10 * r); for (uint c = ; c < 0x10; ++c)
{
uint cur = (first + 0x10 * r + c);
if (cur < start)
{
Console.Write(" {0} ", Convert.ToChar(0x20));
}
else if (end < cur)
{
Console.Write(" {0} ", Convert.ToChar(0x20));
}
else
{
// the cast to int is safe, since we know that val <= upperRange.
String chars = Char.ConvertFromUtf32((int)cur);
// Display a space for code points that are not valid characters.
if (CharUnicodeInfo.GetUnicodeCategory(chars[]) ==
UnicodeCategory.OtherNotAssigned)
Console.Write(" {0} ", Convert.ToChar(0x20));
// Display a space for code points in the private use area.
else if (CharUnicodeInfo.GetUnicodeCategory(chars[]) ==
UnicodeCategory.PrivateUse)
Console.Write(" {0} ", Convert.ToChar(0x20));
// Is surrogate pair a valid character?
// Note that the console will interpret the high and low surrogate
// as separate (and unrecognizable) characters.
else if (chars.Length > && CharUnicodeInfo.GetUnicodeCategory(chars, ) ==
UnicodeCategory.OtherNotAssigned)
Console.Write(" {0} ", Convert.ToChar(0x20));
else
Console.Write(" {0} ", chars);
} switch (c)
{
case :
case :
Console.Write("-");
break;
case :
Console.Write("--");
break;
}
} Console.WriteLine();
if ( < r && r % 0x10 == )
Console.WriteLine();
}
} private static uint RoundUpToMultipleOf(uint b, uint u)
{
return RoundDownToMultipleOf(b, u) + b;
} private static uint RoundDownToMultipleOf(uint b, uint u)
{
return u - (u % b);
} }

演示结果

NET Framework 4.5新特性 (二) 控制台支持 Unicode (UTF-16) 编码的更多相关文章

  1. 精进不休 .NET 4.5 (12) - ADO.NET Entity Framework 6.0 新特性, WCF Data Services 5.6 新特性

    [索引页][源码下载] 精进不休 .NET 4.5 (12) - ADO.NET Entity Framework 6.0 新特性, WCF Data Services 5.6 新特性 作者:weba ...

  2. .Net Framework 各个版本新特性总结 (一)

    .Net Framework 4.5 新特性 最近面试时又看到有问.Net Framework 新特性的问题,一时被问到了.平时也是拿起来就用,新版本出来了,新特性也就是瞄一眼,也没去仔细查看.这次干 ...

  3. NET Framework 4.5新特性 数据库的连接加密保护。

    NET Framework 4.5新特性 (一) 数据库的连接加密保护. NET Framework 4.5 ado.net数据库连接支持使用SecureString内存流方式保密文本.  一旦使用这 ...

  4. NET Framework 4.5新特性 (一) 数据库的连接加密保护。

    NET Framework 4.5 ado.net数据库连接支持使用SecureString内存流方式保密文本.  一旦使用这类操作,文本加密是私有不能共享的,并在不再需要时从计算机内存中删除.  S ...

  5. atitit.js 各版本 and 新特性跟浏览器支持报告

    atitit.js 各版本 and 新特性跟浏览器支持报告 一个完整的JavaScript实现是由以下3个不同部分组成的 •核心(ECMAScript)--JavaScript的核心ECMAScrip ...

  6. ECMAScript 5和ECMAScript6的新特性以及浏览器支持情况

    ECMAScript简介: 它是一种由Ecma国际(前身为欧洲计算机制造商协会)制定和发布的脚本语言规范,javascript在它基础上经行了自己的封装.但通常来说,术语ECMAScript和java ...

  7. ECMAScript和JavaScript的区别,ECMAScript发展更新历史,ECMAScript5和ECMAScript6的新特性及浏览器支持情况,ECMAScript 5/ECMAScript 2015正式发布

    ECMAScript和JavaScript的区别 ECMA是European Computer Manufacturers Association的缩写,即欧洲计算机制造商协会.欧洲计算机制造商协会是 ...

  8. .NET Framework 4.5新特性

    前言 .Net FrameWrok的每个版本都要他的新特性的加入,比如,NET1.1中的委托,NET2.0中的泛型,NET3.0中的Linq,.NET4.0中的动态类型,那么.NET Framewor ...

  9. framework各版本新特性(为面试准备)

    菜鸟D估计描述这些新特性的文章都是烂大街的货色,之所以拿出来分(e)享(xin)一下,有两个原因:1.当年面试的时候有人问到,我不知道该怎么回答:2.项目需要发布了,但是考虑到framework的版本 ...

随机推荐

  1. jqplot使用小心得

    这两天做一个项目,需要画饼图,所以在网上搜到jqplot这个插件.下面就说说我对他的简单的使用心得. 先说说我想要的效果:1.我需要修改饼图每个部分的背景色 2.我需要修改饼图里面文本的颜色和字体大小 ...

  2. 使用httpModules做一些事

    httpmodules是http管道处理程序 可以重写接口进行一些在请求到达api接口前做全局处理 这是一个过滤关键词的例子 using System; using System.Collection ...

  3. SQL中的join操作总结(非常好)

    1.1.1 摘要 Join是关系型数据库系统的重要操作之一,SQL Server中包含的常用Join:内联接.外联接和交叉联接等.如果我们想在两个或以上的表获取其中从一个表中的行与另一个表中的行匹配的 ...

  4. 转 Activity的四种LaunchMode(写的真心不错,建议大家都看看)

      我们今天要讲的是Activity的四种launchMode. launchMode在多个Activity跳转的过程中扮演着重要的角色,它可以决定是否生成新的Activity实例,是否重用已存在的 ...

  5. Tcp Ip -- tcpdump win窗口大小

    问题介绍 今天,有内部模块与外部系统断连. (外部系统smgw,内部接口interface) smgw <----> interface 有消息交互. 通过tcpdump -xns0 po ...

  6. A successful Git branching model/GIT分支管理是一门艺术

    英文原文:http://www.nvie.com/posts/a-successful-git-branching-model/ 原文作者:Vincent Driessen 本文经Linux大棚博主总 ...

  7. 多线程-Condition

    关键字synchronized与wait和notify/notifyAll方法相结合可以实现等待/通知模式,类ReentrantLock也可以实现同样的功能,但需要借助于Condition对象.Con ...

  8. httpclient 优化

    (1)采用单例模式(重用HttpClient实例)    对于一个通信单元甚至是整个应用程序,Apache强烈推荐只使用一个HttpClient的实例.例如: private static HttpC ...

  9. IEnumerable扩展

    void Main() { // This uses a custom 'Pair' extension method, defined below. Customers .Select (c =&g ...

  10. python -&gt; lambda与def的差别

    lambda能够定义一个匿名函数.而def定义的函数必须有一个名字. 这应该是lambda与def两者最大的差别. 与Javascript不同的是,python中匿名函数与非匿名函数须要使用不同的语法 ...