public static string WriteIntelHexFile(byte[] buffer, int recordLength, byte emptyValue) { StringBuilder sb = new StringBuilder(); //Split data to segments by 64K List <byte[]> segments = Tools.ArraySplit(buffer, 64 * 1024); if (segments != null && segments.Count > 0) { for (int i = 0; i < segments.Count; i++) { //Write segment line sb.AppendLine(string.Format(":02000004{0}{1}", Tools.GetHex((ushort)i), Tools.GetHex((byte)(0xFA - i)))); //Split segment data to rows by recordLength List <byte[]> rows = Tools.ArraySplit(segments[i], recordLength); if (rows != null && rows.Count > 0) { short prevRecordLength = 0; for (int j = 0; j < rows.Count; j++) { sb.AppendLine(string.Format(":{0}{1}00{2}{3}", Tools.GetHex((byte)rows[j].Length), Tools.GetHexShort(new byte[] { (byte)(prevRecordLength >> 8), (byte)prevRecordLength }), Tools.GetHex(rows[j]).Replace(" ", ""), Tools.GetHex(CalculateChecksum(rows[j], prevRecordLength)))); prevRecordLength += (short)rows[j].Length; } } } } sb.AppendLine(":00000001FF"); return(sb.ToString()); }
public static string GetHex(this IList <byte> array) { if (array == null || array.Count == 0) { return(string.Empty); } return(Tools.GetHex(array)); }
public static string GetHex(this byte[] array) { if (array == null || array.Length == 0) { return(string.Empty); } return(Tools.GetHex(array)); }
public static string GetHex(this byte value) { return(Tools.GetHex(value, true)); }
public static string WriteIntelHexFile(byte[] buffer, int startAddress, int endAddress, int recordLength, byte emptyValue, bool skipNoDataLines) { StringBuilder sb = new StringBuilder(); List <byte[]> sBuffer = new List <byte[]>(); ushort segNum = 0; if (recordLength == 0) { recordLength = 0x10; } for (int i = 0; i < buffer.Length; i += recordLength) { byte[] arr = new byte[recordLength]; if (i < buffer.Length - recordLength) { Buffer.BlockCopy(buffer, i, arr, 0, recordLength); } else { arr = new byte[buffer.Length - i]; Buffer.BlockCopy(buffer, i, arr, 0, arr.Length); } sBuffer.Add(arr); } if (!skipNoDataLines) { sb.AppendLine(string.Format(":02000004{0}{1}", Tools.GetHex(segNum), Tools.GetHex((byte)(0xFA - segNum)))); } for (int i = 0; i < sBuffer.Count; i++) { byte chksum = (byte)sBuffer[i].Length; chksum += (byte)((byte)((ushort)(i * recordLength) & 0x00FF) + (byte)((ushort)(i * recordLength) >> 8 & 0x00FF)); if (i >> 12 > segNum) { segNum = (ushort)(i >> 12); sb.AppendLine(string.Format(":02000004{0}{1}", Tools.GetHex(segNum), Tools.GetHex((byte)(0xFA - segNum)))); } if (startAddress > 0) { sb.Append(string.Format(":{0}{1}00", Tools.GetHex((byte)sBuffer[i].Length), Tools.GetHex((ushort)(((startAddress + i) * recordLength) & 0x0000FFFF)))); } else { sb.Append(string.Format(":{0}{1}00", Tools.GetHex((byte)sBuffer[i].Length), Tools.GetHex((ushort)((i * recordLength) & 0x0000FFFF)))); } sb.Append(Tools.GetHexShort(sBuffer[i])); for (int j = 0; j < sBuffer[i].Length; j++) { chksum += sBuffer[i][j]; } if (chksum != 0) { sb.Append(Tools.GetHex((byte)(0x100 - chksum))); } else { sb.Append(Tools.GetHex(0x00)); } sb.Append(Environment.NewLine); } if (!skipNoDataLines) { sb.AppendLine(":00000001FF"); } return(sb.ToString()); }