从 .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. win7保护眼睛的颜色设置方法(85,125,205)

    win7保护眼睛的颜色设置方法(85,125,205) 在电脑桌面空白处点击右键“个性化”按钮. 在弹出的对话框单击“窗口颜色”. 在弹出的新的对话框中单击“高级外观设置”. 在弹出的窗口颜色和外观中 ...

  2. REP-20147 Cannot implement column link for this detail SQL query. Converting to group link.

    注:本文为原创,作为学习交流使用,转载请标明作者及出处,作者保留追究法律责任的权力. Lumen Su lzsu1989#gmail.com (#=@) www.cnblogs.com/lzsu198 ...

  3. 几种TCP连接中出现RST的情况(转载)

    TCP RST 网络 linux 目录[-] 1 端口未打开 2 请求超时 3 提前关闭 4 在一个已关闭的socket上收到数据 总结 参考文献: 应该没有人会质疑,现在是一个网络时代了.应该不少程 ...

  4. 经典.net面试题目(转载)

    1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private :   私有成员, 在类的内部才可以访问. protected : 保 ...

  5. window.print

    function contentPrint(contentArea) { var initBody = document.body.innerHTML; if ($("div"). ...

  6. 点滴积累【JS】---JS小功能(JS实现多物体缓冲运动)

    效果: 思路: 利用setInterval计时器进行运动,offsetWidth实现宽度的变动,在用onmouseover将终点和所选中的DIV放入参数再进行缓冲运动. 代码: <head ru ...

  7. redis源码学习_字典

    redis中字典有以下要点: (1)它就是一个键值对,对于hash冲突的处理采用了头插法的链式存储来解决. (2)对rehash,扩展就是取第一个大于等于used * 2的2 ^ n的数作为新的has ...

  8. Postman 常用操作

    一,入参为json 格式 实例:添加学生信息,这个接口是用来讲入参式json 类型的 二,上传文件 三,添加header 信息 实例:获取所有学生信息 四,添加cookie以及身份验证 实例:学生金币 ...

  9. ny82 迷宫寻宝(一) map+queue

    题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=82 AC代码:讲解,先统计在可搜索范围内对应的钥匙数,把搜到的门存到另外的一个队列中,第一 ...

  10. 快速解读GC日志

    本文是 Plumbr 发行的 Java垃圾收集指南 的部分内容.文中将介绍GC日志的输出格式, 以及如何解读GC日志, 从中提取有用的信息.我们通过 -XX:+UseSerialGC 选项,指定JVM ...