Exemple #1
0
 public static byte[] GetBytes(this string hexString)
 {
     return(Tools.GetBytes(hexString));
 }
Exemple #2
0
        private static int ReadHex(byte emptyValue, SortedList <short, List <byte> > result, TextReader sr)
        {
            int    ret = -1;
            short  currentSegmentKey = 0;
            string input;

            while ((input = sr.ReadLine()) != null)
            {
                if (!input.StartsWith(":"))
                {
                    continue;
                }
                byte[] hexRecord = Tools.GetBytes(input.Replace(":", ""));
                if (hexRecord.Length > 3)
                {
                    if (hexRecord[3] == (byte)Records.Data)
                    {
                        if (hexRecord.Length != hexRecord[0] + 5)
                        {
                            throw new FileFormatException("Not Intel HEX file.");
                        }

                        if (result.Count == 0)
                        {
                            result.Add(0, new List <byte>());
                        }

                        ushort address = (ushort)((hexRecord[1] << 8) + hexRecord[2]);
                        if (ret == -1)
                        {
                            ret = address;
                        }

                        if (result[currentSegmentKey].Count - 1 < address)
                        {
                            result[currentSegmentKey]
                            .AddRange(BlankArray(new byte[address + 1 - result[currentSegmentKey].Count],
                                                 emptyValue));
                        }
                        if (result[currentSegmentKey].Count < hexRecord[0] + address)
                        {
                            result[currentSegmentKey].AddRange(BlankArray(new byte[hexRecord[0] - 1], emptyValue));
                        }

                        for (int i = 0; i < hexRecord[0]; i++)
                        {
                            result[currentSegmentKey][address] = hexRecord[i + 4];
                            address++;
                        }
                    }
                    else if (hexRecord[3] == (byte)Records.Eof)
                    {
                        break;
                    }
                    else if (hexRecord[3] == (byte)Records.ExtendedLinearAddr)
                    {
                        if (hexRecord.Length < 7)
                        {
                            throw new FileFormatException("Not Intel HEX file.");
                        }
                        currentSegmentKey = (short)((hexRecord[4] << 8) + hexRecord[5]);
                        result.Add(currentSegmentKey, new List <byte>());
                    }
                    else if (hexRecord[3] == (byte)Records.ExtendedSegmentAddr)
                    {
                        if (hexRecord.Length < 7)
                        {
                            throw new FileFormatException("Not Intel HEX file.");
                        }
                    }
                    else
                    {
                        throw new FileFormatException("Unknown field found. Not Intel HEX file.");
                    }
                }
            }
            return(ret);
        }