本文共 5249 字,大约阅读时间需要 17 分钟。
通用的编码格式为UTF8 ,你也可以修改codingType类型获取想要的编码格式的结果
using UnityEngine.UI;using System;using UnityEngine;////// 编码类型/// public enum CodingType{ UTF8 = 1, ASCII, Unicode, GB,}public class CheckCodingFormat : MonoBehaviour{ ////// 编辑结束回调 /// /// public delegate void CallBackInputContent(string content); public event CallBackInputContent onChangeValueCallBack; public event CallBackInputContent onEndEditCallBack; ////// 限制的字节数 /// public int BYTE_LIMIT = 10; public CodingType codingType = CodingType.UTF8; private InputField inputField; void Awake() { inputField = transform.GetComponent(); inputField.onEndEdit.AddListener(OnEndEdit); } /// /// 实时改变内容时的回调 /// /// private void OnChangedValue(string value) { value = GetCodingResult(codingType); inputField.text = value; if (onChangeValueCallBack != null) { onChangeValueCallBack(value); } } ////// 结束编辑后的回调 /// /// private void OnEndEdit(string value) { value = GetCodingResult(codingType); inputField.text = value; if (onEndEditCallBack != null) { onEndEditCallBack(value); } } ////// 获取编码结果 /// /// ///public string GetCodingResult(CodingType checkType) { string value = ""; string temp = inputField.text.Substring(0, (inputField.text.Length < BYTE_LIMIT + 1) ? inputField.text.Length : BYTE_LIMIT + 1); switch (checkType) { case CodingType.UTF8: value = CodingByUTF8(temp); break; case CodingType.ASCII: value = CodingByASCII(temp); break; case CodingType.Unicode: value = CodingByUnicode(temp); break; case CodingType.GB: value = CodingByGB(temp); break; } return value; } /// /// UTF8编码格式 /// /// ///private string CodingByUTF8(string temp) { string outputStr = ""; int count = 0; for (int i = 0; i < temp.Length; i++) { string tempStr = temp.Substring(i, 1); byte[] encodedBytes = System.Text.ASCIIEncoding.UTF8.GetBytes(tempStr); string output = "[" + temp + "]"; for (int byteIndex = 0; byteIndex < encodedBytes.Length; byteIndex++) { output += Convert.ToString((int)encodedBytes[byteIndex], 2) + " "; } int byteCount = System.Text.ASCIIEncoding.UTF8.GetByteCount(tempStr); // 中文2字节,英文1字节 count = byteCount > 1 ? count += 2 : count += 1; if (count <= BYTE_LIMIT) outputStr += tempStr; else break; } return outputStr; } /// /// Unicode 编码格式 /// /// ///private string CodingByUnicode(string temp) { string outputStr = ""; int count = 0; for (int i = 0; i < temp.Length; i++) { string tempStr = temp.Substring(i, 1); // Unicode用两个字节对字符进行编码 byte[] encodedBytes = System.Text.ASCIIEncoding.Unicode.GetBytes(tempStr); if (encodedBytes.Length == 2) { int byteValue = (int)encodedBytes[1]; if (byteValue == 0)//这里是单个字节 { count += 1; } else { count += 2; } } if (count <= BYTE_LIMIT) { outputStr += tempStr; } else { break; } } return outputStr; } /// /// GB编码格式 /// /// ///private string CodingByGB(string temp) { string outputStr = ""; int count = 0; for (int i = 0; i < temp.Length; i++) { string tempStr = temp.Substring(i, 1); byte[] encodedBytes = System.Text.ASCIIEncoding.Default.GetBytes(tempStr); if (encodedBytes.Length == 1) { count += 1; } else { count += 2; } if (count <= BYTE_LIMIT) { outputStr += tempStr; } else { break; } } return outputStr; } /// /// ASCII编码格式 /// /// ///private string CodingByASCII(string temp) { byte[] encodedBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(temp); string outputStr = ""; int count = 0; for (int i = 0; i < temp.Length; i++) { if ((int)encodedBytes[i] == 63) count += 2; else count += 1; if (count <= BYTE_LIMIT) outputStr += temp.Substring(i, 1); else if (count > BYTE_LIMIT) break; } if (count <= BYTE_LIMIT) { outputStr = temp; } return outputStr; }}
转载地址:http://ulizo.baihongyu.com/