通用的编码格式为UTF8 ,你也可以修改codingType类型获取想要的编码格式的结果
using UnityEngine.UI;
using System;
using UnityEngine;
/// <summary>
/// 编码类型
/// </summary>
public enum CodingType
{
UTF8 = 1,
ASCII,
Unicode,
GB,
}
public class CheckCodingFormat : MonoBehaviour
{
/// <summary>
/// 编辑结束回调
/// </summary>
/// <param name="content"></param>
public delegate void CallBackInputContent(string content);
public event CallBackInputContent onChangeValueCallBack;
public event CallBackInputContent onEndEditCallBack;
/// <summary>
/// 限制的字节数
/// </summary>
public int BYTE_LIMIT = 10;
public CodingType codingType = CodingType.UTF8;
private InputField inputField;
void Awake()
{
inputField = transform.GetComponent<InputField>();
inputField.onEndEdit.AddListener(OnEndEdit);
}
/// <summary>
/// 实时改变内容时的回调
/// </summary>
/// <param name="value"></param>
private void OnChangedValue(string value)
{
value = GetCodingResult(codingType);
inputField.text = value;
if (onChangeValueCallBack != null)
{
onChangeValueCallBack(value);
}
}
/// <summary>
/// 结束编辑后的回调
/// </summary>
/// <param name="value"></param>
private void OnEndEdit(string value)
{
value = GetCodingResult(codingType);
inputField.text = value;
if (onEndEditCallBack != null)
{
onEndEditCallBack(value);
}
}
/// <summary>
/// 获取编码结果
/// </summary>
/// <param name="checkType"></param>
/// <returns></returns>
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;
}
/// <summary>
/// UTF8编码格式
/// </summary>
/// <param name="temp"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Unicode 编码格式
/// </summary>
/// <param name="temp"></param>
/// <returns></returns>
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;
}
/// <summary>
/// GB编码格式
/// </summary>
/// <param name="temp"></param>
/// <returns></returns>
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;
}
/// <summary>
/// ASCII编码格式
/// </summary>
/// <param name="temp"></param>
/// <returns></returns>
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;
}
}