public String Serialize(byte[] source) { int i = 0, currentRecordIndex; Record[] records; String outcome; Record currentRecord; if (source.Length == 0) { return(""); } records = new Record[source.Length / 255 + 2]; currentRecord = null; currentRecordIndex = 0; for (i = 0; i < source.Length; i++) { if (i % 255 == 0) { if (currentRecord != null) { records[currentRecordIndex++] = currentRecord; } currentRecord = new Record(); currentRecord.Type = 0; currentRecord.Data = new byte[255]; currentRecord.Address = (ushort)((i / 255) * (256 + 12)); currentRecord.DataLength = 0; } currentRecord.Data[i % 255] = source[i]; currentRecord.DataLength++; } if (currentRecord.DataLength > 0 && i % 255 != 0) { records[currentRecordIndex] = currentRecord; } var lastRecord = new Record(); lastRecord.Type = 1; lastRecord.Address = 0; lastRecord.DataLength = 0; records[records.Length - 1] = lastRecord; outcome = ""; i = 0; foreach (Record r in records) { outcome += r.ToString(); if (i < records.Length - 1) { outcome += Environment.NewLine; } i++; } return(outcome); }
private bool IsChecksumValid(String line, Record currentRecord) { byte current = Convert.ToByte(line.Substring(DATA_OFFSET + currentRecord.DataLength * 2, CHECKSUM_LENGTH), 16); return(currentRecord.Checksum == current); }