温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#怎么制作unicode到gb2312映射表

发布时间:2021-08-21 11:26:55 来源:亿速云 阅读:165 作者:chen 栏目:互联网科技

这篇文章主要介绍“C#怎么制作unicode到gb2312映射表”,在日常操作中,相信很多人在C#怎么制作unicode到gb2312映射表问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C#怎么制作unicode到gb2312映射表”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        class unit_converter : IComparable<unit_converter>
        {
            public int gb2312 = 0;
            public int unicode = 0;

            public unit_converter()
            {

            }

            public unit_converter(int gb2312_0, int gb2312_1)
            {
                init_with_gb2312(gb2312_0, gb2312_1);
            }

            private int CompareWithUnicode(unit_converter other)
            {
                return unicode.CompareTo(other.unicode);
            }

            private int CompareWithGB2312(unit_converter other)
            {
                return gb2312.CompareTo(other.gb2312);
            }

            public int CompareTo(unit_converter other)
            {
                if(unicode == other.unicode)
                {
                    Console.WriteLine("something wrong with gb2312: "+ gb2312.ToString("X4") + "unicode: "+unicode.ToString("X4"));
                }
                return CompareWithUnicode(other);
                //return CompareWithGB2312(other);
            }

            private byte[] _gb2312 = new byte[2];
            private byte[] _unicode = new byte[2];

            public void init_with_gb2312(int gb2312_0, int gb2312_1)
            {
                _gb2312[0] = (byte)gb2312_0;
                _gb2312[1] = (byte)gb2312_1;
                gb2312 = _gb2312[0] * 256 + _gb2312[1];

                String s_target_gb2312 = System.Text.Encoding.GetEncoding("GB2312").GetString(_gb2312);
                byte[] b_target_unicode = System.Text.Encoding.GetEncoding("unicode").GetBytes(s_target_gb2312);
                _unicode[0] = b_target_unicode[1];
                _unicode[1] = b_target_unicode[0];

                unicode = _unicode[0] * 256 + _unicode[1];
            }

            private void init_with_unicode(int unicode_0, int unicode_1)
            {
                _unicode[0] = (byte)unicode_1;
                _unicode[1] = (byte)unicode_0;
                unicode = _unicode[0] * 256 + _unicode[1];

                String s_target_unicode = System.Text.Encoding.GetEncoding("unicode").GetString(_unicode);
                byte[] b_target_gb2312 = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s_target_unicode);
                _gb2312[0] = b_target_gb2312[0];
                _gb2312[1] = b_target_gb2312[1];
                gb2312 = _gb2312[0] * 256 + _gb2312[1];
            }
        }

        List<unit_converter> elements = new List<unit_converter>();


        private void make_src_gb2312()
        {
            for (int index_level_1 = 0xA1; index_level_1 < 0xAA; index_level_1++)
            {
                for (int index_level_2 = 0xA1; index_level_2 < 0xFF; index_level_2++)
                {
                    elements.Add(new unit_converter(index_level_1, index_level_2));
                }
            }
            for (int index_level_1 = 0xB0; index_level_1 < 0xF7; index_level_1++)
            {
                for (int index_level_2 = 0xA1; index_level_2 <= 0xFF; index_level_2++)
                {
                    elements.Add(new unit_converter(index_level_1, index_level_2));
                }
            }

            Console.WriteLine("CNT " + elements.Count.ToString());

            elements.Sort();

        }


        private String save_to_string()
        {
            String target_string = "";
            target_string += "uint16_t const sorted_unicode[MAX_UNI_INDEX] = {";
            int length = elements.Count();
            for(int i =0; i < length; i++)
            {
                if(i%8 == 0)
                {
                    target_string += "\r\n   ";
                }
                target_string += " 0x" + elements[i].unicode.ToString("X4") + ",";
            }
            target_string += "\r\n};";

            target_string += "\r\nuint16_t const sorted_gb2312[MAX_UNI_INDEX] = {";
            for (int i = 0; i < length; i++)
            {
                if (i % 8 == 0)
                {
                    target_string += "\r\n   ";
                }
                target_string += " 0x" + elements[i].gb2312.ToString("X4") + ",";
            }
            target_string += "\r\n};";
            return target_string;
        }

        private void save_to_file()
        {
            String t = save_to_string();
        }

        private void font_test()
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //C9AD  森
            byte[] b_src_gb2312 = new byte[2] { 0xC9, 0xAD };


            byte[] b_src_unicode = new byte[2] { 0x02, 0x30 };
            unit_converter converter = new unit_converter(b_src_gb2312[0], b_src_gb2312[1]);

            //String s_target_gb2312 = System.Text.Encoding.GetEncoding("GB2312").GetString(b_src_gb2312);
            //byte[] b_target_unicode = System.Text.Encoding.GetEncoding("unicode").GetBytes(s_target_gb2312);
            //String s_target_unicode = System.Text.Encoding.GetEncoding("unicode").GetString(b_target_unicode);
            String s_target_unicode = System.Text.Encoding.GetEncoding("unicode").GetString(b_src_unicode);

            byte[] b_target_gb2312 = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s_target_unicode);
            make_src_gb2312();
            save_to_file();
            font_test();

            Console.WriteLine("start");
        }
    }
}

到此,关于“C#怎么制作unicode到gb2312映射表”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI