using System; using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// 通用工具 /// public class CommonTools { /// /// 获取角度 /// . /// 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; } /// /// 获取数据 /// /// /// /// public static float GetShuJu(string data1, string data2) { return Convert.ToInt32(data1, 16) * 256 + Convert.ToInt32(data2, 16); } /// /// 获取角度 /// public static Quaternion GetEuler(float pitch, float yaw, float roll) { Vector3 eulerAngles2 = new(pitch, yaw, roll); return Quaternion.Euler(eulerAngles2); } #region 16进制转换相关 /// /// 字节数组转16进制字符串 /// 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; } /// /// 字符串转16进制 /// /// 要转格式的字符串 /// 转化为16进制的字符串 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; } /// /// 16进制转回字符串 /// /// 16进制 /// 转回的字符串 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; } /// /// 字符串转字节流 /// /// /// 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; } /// /// 字符串转字节流 -- 推荐用法 /// 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 }