using System.Collections; using System.Collections.Generic; using UnityEngine; using LitJson; using System; namespace JXSoft { //协议规则解析通用方法扩展 public static class ProtocolRulesExtention { /// /// Hex数据(字符串)转Byte数组,用于Hex类通讯协议 /// /// 只能包含A-F,0-9,hex /// 起始位置 /// 长度 /// public static byte[] toByteArrayByHex(this string self) { int hexlen = self.Length; byte[] result; if (hexlen % 2 == 1) { //奇数 hexlen++; result = new byte[(hexlen / 2)]; self += "0" ; } else { //偶数 result = new byte[(hexlen / 2)]; } int j = 0; for (int i = 0; i < hexlen; i += 2) { result[j] = (byte)int.Parse(self.Substring(i, 2), System.Globalization.NumberStyles.HexNumber); j++; } return result; } /// /// Json数据转对象 /// /// 数据类型 /// /// public static TObject toObjectByJson(this string self) { return JsonMapper.ToObject(self); } } }