/// <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); }
/// <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); }
public static object ParseArrayTypeFromString(string rawval, string type, string format) { string str = rawval.Trim(); try { switch (type) { case "u1v": case "u96": switch (format) { case "Hex": return((object)LLRPBitArray.FromHexString(str)); default: return((object)LLRPBitArray.FromString(str)); } case "bytesToEnd": case "u8v": switch (format) { case "Hex": return((object)ByteArray.FromHexString(str)); default: return((object)ByteArray.FromString(str)); } case "s8v": switch (format) { case "Hex": return((object)SignedByteArray.FromHexString(str)); default: return((object)SignedByteArray.FromString(str)); } case "u16v": switch (format) { case "Hex": return((object)UInt16Array.FromHexString(str)); default: return((object)UInt16Array.FromString(str)); } case "s16v": switch (format) { case "Hex": return((object)Int16Array.FromHexString(str)); default: return((object)Int16Array.FromString(str)); } case "u32v": switch (format) { case "Hex": return((object)UInt32Array.FromHexString(str)); default: return((object)UInt32Array.FromString(str)); } case "s32v": switch (format) { case "Hex": return((object)Int32Array.FromHexString(str)); default: return((object)Int32Array.FromString(str)); } case "utf8v": return((object)str); default: throw new Exception(string.Format("{0} is unsupported type.", (object)type)); } } catch { throw new Exception(string.Format("Can't parse {0} to {1} as format {2}", (object)str, (object)type, (object)format)); } }