Unity中对输入的字符进行Unicode转码,并转回正常字符串 以及 GB2312转UTF-8 MD5码的转换

最近有个小的新需求,就是需要在程序中对Unicode进行转码.. 网上也有各种大神的教程..

https://blog.csdn.net/u011511086/article/details/72865006/

然后我这里就模仿这个大神,做了一个小demo.. 源码如下...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Text;
using UnityEngine.UI;
using System.Security.Cryptography;

public class UnCodeTest : MonoBehaviour
{
	Button btn1;
	Button btn2;
	InputField iField1;
	InputField iField2;

	void Start ()
	{
		btn1 = transform.Find ("Button1").GetComponent <Button> ();
		btn2 = transform.Find ("Button2").GetComponent <Button> ();
		iField1 = transform.Find ("InputField1").GetComponent <InputField> ();
		iField2 = transform.Find ("InputField2").GetComponent <InputField> ();

		btn1.onClick.AddListener (delegate() {
			string temp = iField1.text;
			string debug1 = StringToUnicode (temp);
			Debug.Log ("debug1 = " + debug1);
			string debug2 = UnicodeToString (debug1);
			Debug.Log ("debug2 = " + debug2);
		});

		btn2.onClick.AddListener (delegate() {
			string temp = iField1.text;
			string debug1 = GB2312ToUTF8 (temp);
			Debug.Log ("debug1 = " + debug1);
			string debug2 = UTF8ToGB2312 (debug1);
			Debug.Log ("debug2 = " + debug2);

			string temp2 = iField2.text;
			string debug3 = GetMD532 (temp2);
			Debug.Log ("debug3 = " + debug3);
		});
	}

	/// <summary>  
	/// 字符串转为UniCode码字符串  
	/// </summary>  
	/// <param name="s"></param>  
	/// <returns></returns>  
	public static string StringToUnicode (string inputText)
	{
		char[] charBuffer = inputText.ToCharArray ();
		byte[] buffer;
		StringBuilder stringBuilder = new StringBuilder ();
		for (int i = 0; i < charBuffer.Length; i++) {
			buffer = System.Text.Encoding.Unicode.GetBytes (charBuffer [i].ToString ());
			stringBuilder.Append (string.Format ("\\u{0:X2}{1:X2}", buffer [1], buffer [0]));
		}
		return stringBuilder.ToString ();
	}

	/// <summary>  
	/// Unicode字符串转为正常字符串  
	/// </summary>  
	/// <param name="srcText"></param>  
	/// <returns></returns>  
	public static string UnicodeToString (string srcText)
	{
		string descripeText = "";
		string src = srcText;
		int len = srcText.Length / 6;
		for (int i = 0; i <= len - 1; i++) {
			string str = "";
			str = src.Substring (0, 6).Substring (2);
			src = src.Substring (6);
			byte[] bytes = new byte[2];
			bytes [1] = byte.Parse (int.Parse (str.Substring (0, 2), System.Globalization.NumberStyles.HexNumber).ToString ());
			bytes [0] = byte.Parse (int.Parse (str.Substring (2, 2), System.Globalization.NumberStyles.HexNumber).ToString ());
			descripeText += Encoding.Unicode.GetString (bytes);
		}
		return descripeText;
	}

	/// <summary>
	/// GB2312 转 UTF-8
	/// </summary>
	/// <returns>The b2312 to UT f8.</returns>
	/// <param name="srcText">Source text.</param>
	public static string GB2312ToUTF8 (string srcText)
	{
		Encoding utf8;
		Encoding gb2312;
		utf8 = Encoding.GetEncoding ("UTF-8");
		gb2312 = Encoding.GetEncoding ("GB2312");
		byte[] GB = gb2312.GetBytes (srcText);
		GB = Encoding.Convert (gb2312, utf8, GB);
		return utf8.GetString (GB);
	}

	/// <summary>
	/// UTF-8 转 GB2312 
	/// </summary>
	/// <returns>The f8 to G b2312.</returns>
	/// <param name="srcText">Source text.</param>
	public static string UTF8ToGB2312 (string srcText)
	{
		byte[] utf = Encoding.GetEncoding ("UTF-8").GetBytes (srcText);
		utf = Encoding.Convert (Encoding.GetEncoding ("UTF-8"), Encoding.GetEncoding ("GB2312"), utf);
		return Encoding.GetEncoding ("GB2312").GetString (utf);
	}

	/// <summary>
	/// MD5码
	/// </summary>
	/// <returns>The M d532.</returns>
	/// <param name="content">Content.</param>
	public static string GetMD532 (string content)
	{
		string cl = content;
		StringBuilder pwd = new StringBuilder ();
		MD5 md5 = MD5.Create ();//实例化一个md5对像
		// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
		byte[] s = md5.ComputeHash (Encoding.UTF8.GetBytes (cl));
		// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
		for (int i = 0; i < s.Length; i++) {
			// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
			pwd.Append (s [i].ToString ("X2"));
			//pwd = pwd + s[i].ToString("X");
		}
		return pwd.ToString ();
	}
}


IField2 中对 InputField 中的属性更改,给他设置为Password 参考如图 

属性设置如右图 : 

 

InputField  教程如下:

https://www.cnblogs.com/jiangyuzhen/p/7149484.html?utm_source=itdadao&utm_medium=referral

 

输入参数如下:

 

打印信息如下:    \u8FD9\u662F\u4E00\u4E2A\u6D4B\u8BD5

Unicode转码网址如下 : http://tool.chinaz.com/tools/unicode.aspx  MD5转码网址:http://www.cmd5.com/

这个时候,转码出来的打印信息...

 

posted @ 2018-08-06 15:09  不够自律的人  阅读(3684)  评论(0编辑  收藏  举报