加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#计算输入汉字GBK编码后十六进制数输出的方法

发布时间:2020-12-15 05:49:47 所属栏目:百科 来源:网络整理
导读:本篇章节讲解C#计算输入汉字GBK编码后十六进制数输出的方法。供大家参考研究。具体实现方法如下: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace getCode{ class Pro

本篇章节讲解C#计算输入汉字GBK编码后十六进制数输出的方法。分享给大家供大家参考。具体实现方法如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace getCode
{
 class Program
 { 
  /// <summary>
  /// 计算输入汉字对应的GBK编码主函数入口
  /// </summary>
  /// <param name="args">< /param> static void Main(string[] args)
  {
   try
   {
    while (true)
    {
     char cWord = (char)Console.Read();
    //读入一个汉字
     byte[] bGb2312 = Encoding.GetEncoding("gbk").GetBytes(new Char[] { cWord });
    //<span id="mt1" class="sentence" data-guid="21b4c66a1cae34f4812147989864f337" data-source="Returns the encoding associated with the specified code page name.">返回gbk的编码</span>
    //对象的字节数组,包含对cWord进行编码的结果。
     int n = (int)bGb2312[0] << 8;
    //第一个字节左移八位
     n += (int)bGb2312[1];
     Console.WriteLine("汉字{0}的gbk编码为:{1,4:x4}",cWord,n);
    //输出汉字对应是十六进制GBK编码
     Console.ReadKey();
    }
   }
   catch
   {
    Console.WriteLine("输入错误!请输入汉字字符!");
   }
  }
 }
}

希望本文所述对大家的C#程序设计有所帮助。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读