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

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

发布时间:2020-12-15 17:51:49 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 C# 计算输入汉字的GBK编码,十六进制数输出 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Thre

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

C# 计算输入汉字的GBK编码,十六进制数输出
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace getCode
{
    class Program
    { 
&nbsp;       /// <summary>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// 计算输入汉字对应的GBK编码主函数入口
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// </summary>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// <param name="args">< /param>&nbsp;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("输入错误!请输入汉字字符!");
          }
        }
    }
}
 

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

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

    推荐文章
      热点阅读