136 lines
3.8 KiB
C#
136 lines
3.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 通用工具
|
|
/// </summary>
|
|
public class CommonTools
|
|
{
|
|
/// <summary>
|
|
/// 获取角度
|
|
/// </summary>.
|
|
/// <returns></returns>
|
|
public static float GetDu(string data1, string data2)
|
|
{
|
|
float loData = (Convert.ToInt32(data1, 16) << 8 | Convert.ToInt32(data2, 16)) / 100f;
|
|
if (loData > 360)
|
|
{
|
|
loData = 360 - (Convert.ToInt32("0xffff", 16) - (Convert.ToInt32(data1, 16) << 8 | Convert.ToInt32(data2, 16))) / 100f;
|
|
}
|
|
return loData;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取数据
|
|
/// </summary>
|
|
/// <param name="data1"></param>
|
|
/// <param name="data2"></param>
|
|
/// <returns></returns>
|
|
public static float GetShuJu(string data1, string data2)
|
|
{
|
|
return Convert.ToInt32(data1, 16) * 256 + Convert.ToInt32(data2, 16);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取角度
|
|
/// </summary>
|
|
public static Quaternion GetEuler(float pitch, float yaw, float roll)
|
|
{
|
|
Vector3 eulerAngles2 = new(pitch, yaw, roll);
|
|
return Quaternion.Euler(eulerAngles2);
|
|
}
|
|
#region 16进制转换相关
|
|
/// <summary>
|
|
/// 字节数组转16进制字符串
|
|
/// </summary>
|
|
public static string byteToHexStr(byte[] bytes)
|
|
{
|
|
string returnStr = "";
|
|
if (bytes != null)
|
|
{
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
{
|
|
returnStr += bytes[i].ToString("X2");
|
|
returnStr += "-";
|
|
}
|
|
}
|
|
return returnStr;
|
|
}
|
|
/// <summary>
|
|
/// 字符串转16进制
|
|
/// </summary>
|
|
/// <param name="input">要转格式的字符串</param>
|
|
/// <returns>转化为16进制的字符串</returns>
|
|
private static string ToSixteen(string input)
|
|
{
|
|
char[] values = input.ToCharArray();
|
|
string end = string.Empty;
|
|
foreach (char letter in values)
|
|
{
|
|
int value = Convert.ToInt32(letter);
|
|
string hexoutput = string.Format("{0:X}", value); //0 表示占位符 x或X表示十六进制
|
|
end += hexoutput + "_";
|
|
}
|
|
end = end.Remove(end.Length - 1);
|
|
return end;
|
|
}
|
|
/// <summary>
|
|
/// 16进制转回字符串
|
|
/// </summary>
|
|
/// <param name="input">16进制</param>
|
|
/// <returns>转回的字符串</returns>
|
|
private static string ToMyString(string input)
|
|
{
|
|
string[] hexvaluesplit = input.Split('_');
|
|
string end = string.Empty;
|
|
foreach (string hex in hexvaluesplit)
|
|
{
|
|
int value = Convert.ToInt32(hex, 16);
|
|
string stringvalue = char.ConvertFromUtf32(value);
|
|
char charValue = (char)value;
|
|
end += charValue;
|
|
}
|
|
return end;
|
|
}
|
|
/// <summary>
|
|
/// 字符串转字节流
|
|
/// </summary>
|
|
/// <param name="hexStr"></param>
|
|
/// <returns></returns>
|
|
public static byte[] HexStringToBytes(string hexStr)
|
|
{
|
|
if (string.IsNullOrEmpty(hexStr))
|
|
{
|
|
return new byte[0];
|
|
}
|
|
if (hexStr.StartsWith("0x"))
|
|
{
|
|
hexStr = hexStr.Remove(0, 2);
|
|
}
|
|
var count = hexStr.Length;
|
|
var byteCount = count / 2;
|
|
var result = new byte[byteCount];
|
|
for (int ii = 0; ii < byteCount; ++ii)
|
|
{
|
|
var tempBytes = Byte.Parse(hexStr.Substring(2 * ii, 2), System.Globalization.NumberStyles.HexNumber);
|
|
result[ii] = tempBytes;
|
|
}
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 字符串转字节流 -- 推荐用法
|
|
/// </summary>
|
|
public static byte[] Convert16(string strText)
|
|
{
|
|
strText = strText.Replace(" ", "");
|
|
byte[] bText = new byte[strText.Length / 2];
|
|
for (int i = 0; i < strText.Length / 2; i++)
|
|
{
|
|
bText[i] = Convert.ToByte(Convert.ToInt32(strText.Substring(i * 2, 2), 16));
|
|
}
|
|
return bText;
|
|
}
|
|
#endregion
|
|
} |