从 .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. Linux 网卡丢包严重

    http://hi.baidu.com/scstwy/item/cad0fbef1fdc18d3eb34c9d9

  2. Android View Attributes

    ImageView android:adjustViewBounds  Set this to true if you want the ImageView to adjust its bounds ...

  3. Inno Setup 打包的文件以管理员权限执行

    最近发现一个问题,就是Inno Setup打包的程序安装完毕后执行需求管理员权限的程序的时候会失败( inno createprocess   须要提升),解决问题的最简单办法就是打包的后的程序也以管 ...

  4. Exif.js获取图片的详细信息(苹果手机移动端上传图片旋转90度)

    Exif.js插件介绍 http://code.ciaoca.com/javascript/exif-js/ iOS手机竖着拍的照片经过前端处理之后被旋转了90°的原因以及解决方案 https://w ...

  5. mysql 常用功能

    一.备份 mysqldump [OPTIONS] database [tables] http://www.blogjava.net/Alpha/archive/2007/08/10/135694.h ...

  6. Spring Boot(三):logback打印日志

    springboot对logback的支持是非常好的,不需要任何配置,只需要在resource下加logback.xml就可以实现功能直接贴代码: <?xml version="1.0 ...

  7. Atitit.php  nginx页面空白 并返回500的解决

    Atitit.php  nginx页面空白 并返回500的解决 1.1. 空白问题起源1 1.2. Php.ini 开启display_err1 1.3. 修改www.conf ,并重启动.重启php ...

  8. android studio - installation failed with message Invalid File

    今天将windows上的as项目移动到mac下,使用mac下的as编译时出现下列错误: 解决办法: 1.点击工具栏上的Build中的Clean Project 2.再点击工具栏上的Build中的Reb ...

  9. python学习之endswith()

    定义: Python endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False.可选参数"start"与"end&q ...

  10. HTML5 多图上传

    HTML5 多图上传 时间 2014-06-05 16:06:29  月小升博客 原文  http://java-er.com/blog/html5-many-image-upload/ 主题 HTM ...