/// <summary> /// Convert a Dec string to byte array. /// </summary> /// <param name="str">Dec string. For example: "0 255" or "12,111"</param> /// <returns></returns> public static SignedByteArray FromString(string str) { SignedByteArray bA = new SignedByteArray(); str = str.Trim(); string[] s = str.Split(new char[] { ',', ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); sbyte[] bD = new sbyte[s.Length]; for (int i = 0; i < s.Length; i++) { try { bA.Add((sbyte)SByte.Parse(s[i], System.Globalization.NumberStyles.Integer)); } catch { bA.Add(0); } } return(bA); }
/// <summary> /// Convert a enumeration string to byte array. /// </summary> /// <param name="str"></param> /// <returns></returns> public static SignedByteArray FromString(string str, Type t) { SignedByteArray bA = new SignedByteArray(); str = str.Trim(); string[] s = str.Split(new char[] { ',', ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); sbyte[] bD = new sbyte[s.Length]; for (int i = 0; i < s.Length; i++) { try { bA.Add((sbyte)((int)Enum.Parse(t, s[i], true))); } catch { bA.Add(0); } } return(bA); }
/// <summary> /// Convert a Hex string to byte array. /// </summary> /// <param name="str">Hex string. For example: "EE FF" or "EEFF" or "EE,FF"</param> /// <returns></returns> public static SignedByteArray FromHexString(string str) { SignedByteArray bA = new SignedByteArray(); str = str.Trim(); string[] s = Util.SplitString(str, new char[] { ',', ' ', '\t', '\n', '\r' }, 2); sbyte[] bD = new sbyte[s.Length]; for (int i = 0; i < s.Length; i++) { try { bA.Add((sbyte)SByte.Parse(s[i], System.Globalization.NumberStyles.HexNumber)); } catch { bA.Add(0); } } return(bA); }