C#计算输入汉字GBK编码后十六进制数输出的方法
本文实例讲述了C#计算输入汉字GBK编码后十六进制数输出的方法。分享给大家供大家参考。具体实现方法如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; namespacegetCode { classProgram { ///<summary> ///计算输入汉字对应的GBK编码主函数入口 ///</summary> ///<paramname="args"></param>staticvoidMain(string[]args) { try { while(true) { charcWord=(char)Console.Read(); //读入一个汉字 byte[]bGb2312=Encoding.GetEncoding("gbk").GetBytes(newChar[]{cWord}); //<spanid="mt1"class="sentence"data-guid="21b4c66a1cae34f4812147989864f337"data-source="Returnstheencodingassociatedwiththespecifiedcodepagename.">返回gbk的编码</span> //对象的字节数组,包含对cWord进行编码的结果。 intn=(int)bGb2312[0]<<8; //第一个字节左移八位 n+=(int)bGb2312[1]; Console.WriteLine("汉字{0}的gbk编码为:{1,4:x4}",cWord,n); //输出汉字对应是十六进制GBK编码 Console.ReadKey(); } } catch { Console.WriteLine("输入错误!请输入汉字字符!"); } } } }
希望本文所述对大家的C#程序设计有所帮助。