这篇文章主要介绍“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"); } } }郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。