从 .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. 一些制作app的软件

    搜狐快站 http://www.kuaizhan.com/百度siteapp http://siteapp.baidu.com/腾讯风铃 http://fl.qq.com/

  2. 对oracle数字类型的研究

    Oracle的数字类型主要有number,binary_float,binary_double三类,其他的像int,number(p,s)等等大多数都是由这些引申出来的.这部分的理解主要来自oracl ...

  3. unity, 在surface shader中访问顶点色

    //ref: Custom data computed per-vertex: http://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html ...

  4. Maven实战(一)搭建Nexus伺服器

    在搭建伺服器之前我们先要说明一下为什么要搭建伺服器以及伺服器的作用是什么.在进行分布式开发中maven工具的使用可以极大的提高我们管理项目颗粒的效率,既然是管理颗粒那总得有地方存放才行,而伺服器扮演的 ...

  5. 点滴积累【other】---存储过程删除所有表中的数据(sql)

    USE [QG_Mis24] GO /****** Object: StoredProcedure [dbo].[p_set1] Script Date: 07/18/2013 13:25:57 ** ...

  6. atitit.系统托盘图标的设计java swing c# .net c++ js

    atitit.系统托盘图标的实现java swing c# .net c++ js 1. 系统托盘图标的结构 1 2. Java swing的实现 1 3. .net的实现 1 4. C++的实现 1 ...

  7. 日志分析工具--GoAccess的安装部署

    需求:及时得到线上用户访问日志分析统计结果,以便给开发.测试.运维.运营人员提供决策! 方案:GoAccess,图文并茂,而且速度快,每秒8W 的日志记录解析速度,websocket10秒刷新统计数据 ...

  8. QT界面 理解QStyle和QStyleOption以及QStyleFactory

    QStyleOption类和QStyle类简介 QStyleOption类存储QStyle函数使用的参数.QStyleOption及其子类包含了QStyle函数绘制图形元素所需的所有信息. 由于性能原 ...

  9. myeclipce怎么破解

    MyEclipse安装文件下载,下载地址 http://www.jb51.net/softs/150886.html 你也可以进入官方网站下载:http://www.myeclipsecn.com/d ...

  10. c3p0 APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks

    2018-01-04 15:02:03,319 ---com.mchange.v2.async.ThreadPoolAsynchronousRunner: com.mchange.v2.async.T ...