Example #1
0
        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());
        }