文本与Unicode互转工具

在使用部分软件的时候涉及到一个不可避免的问题,就是不同编码格式下解析的中文乱码问题。

在分析了EAS枚举类型的时候发现使用了unicode编码,于是决定以后的代码中采用同样的方式。

在百度中没有搜索到合适的在线转换工具,于是用vs2017自己做了一个,实现了字符转与unicode格式的互转。

截图如下:

字符串转换成Unicode(一般保留16进制unicode)

字符串中存在\u字符串时建议使用16进制unicode进行逆向转换

 

 Unicode转字符串

 附录C#源码

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

namespace w2u
{
    public partial class From : Form
    {
        public From()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            string str = tbInput.Text;
            if (string.IsNullOrEmpty(str))
            {
                MessageBox.Show("请输入你要转换的字符串");
                tbSix.Text = null;
                tbSixU.Text = null;
                tbInput.Focus();
                return;
            }
            else
            {
                string[] outText = CharacterToCoding(str);
                tbSix.Text = outText[0];
                tbSixU.Text = outText[1];
                Console.WriteLine("转换完成");
                tbInput.Focus();
            }
        }

        private void btnConfirm2_Click(object sender, EventArgs e)
        {
            string str = tbInput.Text;
            if (string.IsNullOrEmpty(str))
            {
                MessageBox.Show("请输入你要转换的字符串");
                tbOutput.Text = null;
                tbInput.Focus();
                return;
            }
            else
            {
                tbOutput.Text = UnicodeToString(str);
                Console.WriteLine("逆向转换完成");
                tbInput.Focus();
            }
        }


        /**
       * 字符串转unicode
       */
        public string[] CharacterToCoding(string character)
        {
            StringBuilder coding = new StringBuilder();
            StringBuilder codingU = new StringBuilder();
            for (int i = 0; i < character.Length; i++)
            {
                byte[] bytes = System.Text.Encoding.Unicode.GetBytes(character.Substring(i, 1));

                //取出二进制编码内容  
                string lowCode = System.Convert.ToString(bytes[0], 16);

                //取出低字节编码内容(两位16进制)  
                if (lowCode.Length == 1)
                {
                    lowCode = "0" + lowCode;
                }

                string hightCode = System.Convert.ToString(bytes[1], 16);

                //取出高字节编码内容(两位16进制)  
                if (hightCode.Length == 1)
                {
                    hightCode = "0" + hightCode;
                }

                coding.Append(hightCode.ToUpper()).Append(lowCode.ToUpper());
                codingU.Append("\\u").Append(hightCode.ToUpper()).Append(lowCode.ToUpper());
            }

            return new string[] { coding.ToString(), codingU.ToString() };
        }
        /**
         * unicode转字符串
         */
        public static string UnicodeToString(string srcText)
        {
            StringBuilder unicodeStr = new StringBuilder();
            srcText.ToCharArray();
            if (!srcText.Contains("\\u"))
            {
                for (int i = 0, size = srcText.Length; i < size; i++)
                {
                    if (i % 4 == 0)
                    {
                        unicodeStr.Append("\\u");
                    }
                    unicodeStr.Append(srcText.Substring(i, 1));
                }
                return UnicodeToString(unicodeStr.ToString());
            }
            else {
                unicodeStr.Append(srcText);
            }
            StringBuilder dst = new StringBuilder();
            String actStr = unicodeStr.ToString();
            Console.WriteLine(actStr);
            int len = srcText.Length / 6;
            for (int i = 0; i <= len - 1; i++)
            {
                string str = "";
                str = actStr.Substring(0, 6).Substring(2);
                actStr = actStr.Substring(6);
                byte[] bytes = new byte[2];
                bytes[1] = byte.Parse(int.Parse(str.Substring(0, 2), NumberStyles.HexNumber).ToString());
                bytes[0] = byte.Parse(int.Parse(str.Substring(2, 2), NumberStyles.HexNumber).ToString());
                dst.Append(Encoding.Unicode.GetString(bytes));
            }
            return dst.ToString();
        }
    }
}

 

posted @ 2022-02-18 10:01  liangda  阅读(615)  评论(0)    收藏  举报