1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
static public string ModelListToHexString(List<IEstimateModel> modelList) { using (var memoryStream = new MemoryStream()) { var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); formatter.Serialize(memoryStream, modelList); return BitConverter.ToString(memoryStream.ToArray()).Replace("-", ""); } } // 16進数文字列>バイト配列>インスタンスリスト static public List<IEstimateModel> HexStringToModelList(string hexString) { List<byte> byteList = new List<byte>(); for (int i = 0; i < hexString.Length; i += 2) { byteList.Add(byte.Parse(hexString.Substring(i, 2),System.Globalization.NumberStyles.AllowHexSpecifier)); } using (var memoryStream = new MemoryStream(byteList.ToArray())) { memoryStream.Seek(0, SeekOrigin.Begin); var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); try { return (List<IEstimateModel>)formatter.Deserialize(memoryStream); } catch { return new List<IEstimateModel>(); } } } |