Reading .hex File with C# File.ReadAllBytes()

477 views Asked by At

Im Working on a Program that Write One Page on MCUs . I Managed to Transfer one Page (128 byte) with fixed values (0x11) to MCU . now i want to read *.hex file and copy its content to byte[128] and then transfer it . but Problem is , Value that Progsip is showing in Buffer is Different with Values that i read from File .

Real Values that i expect to read :

Values with c# (File.ReadAllBytes()):

if you open hex file with Progisp , Buffer Page will Show you First Picture . Now open file with NotePad++ and Convert All of it to Hex (ASCI -> Hex) with Hex Plugin . it shows Second Picture That is the same with C# File.ReadAllBytes(). what makes Difference between Two Softwares ? (NotePad++ & Progisp)

Hex File Link

I opened file with NotePad++ and here it is :

:100000000C942A000C9447000C9447000C94470071
:100010000C9447000C9447000C9447000C94470044
:100020000C9447000C9447000C9447000C94470034
:100030000C9447000C9471010C9447000C944700F9
:100040000C9447000C9447000C9447000C94470014
:100050000C94470011241FBECFE5D8E0DEBFCDBF12
:1000600010E0A0E6B0E0EEEAF3E002C005900D92E9
:10007000A03AB107D9F720E0A0EAB0E001C01D9294
:10008000A23AB207E1F70E949A010C94D5010C94B0
:100090000000009751F0019779F488B3816088BB24
:1000A00082E08A95F1F700C0089588B38E7F88BBFF
:1000B00082E08A95F1F700C00895009751F001970A
:1000C00079F488B3826088BB82E08A95F1F700C03A
:1000D000089588B38D7F88BB82E08A95F1F700C0D0
:1000E0000895009751F0019779F488B3846088BB34
:1000F00082E08A95F1F700C0089588B38B7F88BBB2
:1001000082E08A95F1F700C00895CF93C82F8FEF52
:100110008ABB6115710521F06130710531F009C0AC
:1001200080E090E00E94490004C081E090E00E94DD
:10013000490080E090E00E945D0081E090E00E9434
:10014000710082E08A95F1F700C0CBBB80E090E0BF
:100150000E9471008FEC97E00197F1F700C000005A
:1001600081E090E00E94710092E09A95F1F700C062
:10017000CF910895CF93E091A100E034A1F0C82F72
:10018000F0E0E05AFF4F808160E070E080580E940C
:10019000850061E070E08C2F0E9485008091A100B5
:1001A0008F5F8093A100CF910895CF93803108F0A5
:1001B00045C0643008F042C0C82F61110DC0E82F5F
:1001C000F0E0E05AFF4F808160E070E080580E94CC
:1001D0008500C093A10032C0613079F4E82FF0E0CF
:1001E000E059FF4F808160E070E080580E948500F8
:1001F00080E18C0F8093A10021C0623079F4E82F58
:10020000F0E0E058FF4F808160E070E080580E948D
:10021000850080E28C0F8093A10010C0633071F4E0
:10022000E82FF0E0E057FF4F808160E070E08058F9
:100230000E94850080E38C0F8093A100CF910895E8
:10024000009741F0019759F460E070E08CE00E9463
:100250008500089560E070E088E00E9485000895C0
:1002600060E070E081E00E9485008FE99FE00197E7
:10027000F1F700C000001092A10008952FEF80E771
:1002800092E0215080409040E1F700C0000087B329
:10029000876087BB60E070E088E30E94850081E0B2
:1002A00090E00E9420010E94300160E070E086E052
:1002B0000E9485001092A10008958BB18BB98AB17C
:1002C000886D8AB980B58E6A80BD10BC83E389B918
:1002D00008955D9BFECF8CB908955F9BFECF8CB1D6
:1002E00008951F920F920FB60F9211242F933F93F0
:1002F0004F935F936F937F938F939F93AF93BF932E
:10030000EF93FF93F8948CB18093A0000E94BA0001
:100310007894FF91EF91BF91AF919F918F917F91D1
:100320006F915F914F913F912F910F900FBE0F9062
:100330001F901895CF930E945D010E943E0160E0DE
:1003400087E00E94D50087B3886087BB88B3886048
:1003500088BB2FEF89E698E1215080409040E1F77B
:1003600000C000008FE40E94BA008EE40E94BA0030
:100370000E946D018093A000C0E005C08091A000A4
:100380000E946901CF5FCA30C8F32FEF8DE399E473
:10039000215080409040E1F700C0000088B3877F83
:0E03A00088BB80E090E0CF910895F894FFCFE5
:1003AE00000102030405060708090A0B0C0D0E0FC7
:1003BE00404142434445464748494A4B4C4D4E4FB7
:1003CE00101112131415161718191A1B1C1D1E1FA7
:1003DE00505152535455565758595A5B5C5D5E5F97
:00000001FF
1

There are 1 answers

0
AudioBubble On BEST ANSWER

I Searched in web and find out what problem is . so i write a HexToBin Function to do the things :

public byte AsciiToByte(byte ch)
    {
        byte ret = 0;
        switch(ch)
        {
            case 48:
                ret = 0;
                break;
            case 49:
                ret = 1;
                break;
            case 50:
                ret = 2;
                break;
            case 51:
                ret = 3;
                break;
            case 52:
                ret = 4;
                break;
            case 53:
                ret = 5;
                break;
            case 54:
                ret = 6;
                break;
            case 55:
                ret = 7;
                break;
            case 56:
                ret = 8;
                break;
            case 57:
                ret = 9;
                break;
            case 65:
                ret = 10;
                break;
            case 66:
                ret = 11;
                break;
            case 67:
                ret = 12;
                break;
            case 68:
                ret = 13;
                break;
            case 69:
                ret = 14;
                break;
            case 70:
                ret = 15;
                break;
        }
        return ret;
    }



    public int HexToByteArray(ref byte[] ByteArr , string FileName,ref int CurrentRecord, ref int EndOfFile)
    {
        byte[] Byte = new byte[PageSize * PageNumber];
        for(int a = 0; a < Byte.Length; a++)
        {
            Byte[a] = 0xFF;
        }
        long MaxPoint = (PageNumber * PageSize);
        byte[] file = File.ReadAllBytes(FileName);
        int Offset = 0;
        int BytePointer = 0;
        int RecordSize = 0;
        int CheckSum = 0;
        int CheckSumT = 0;
        int CheckSumM = 0;
        CurrentRecord = 1;
        int LastBytePoint = 0;

        for (int i = 0; i < file.Length; )
        {
            if (file[i] != ':') return 1;

            i++;
            RecordSize = AsciiToByte(file[i]) * 16;
            i++;
            RecordSize += AsciiToByte(file[i]);
            i++;
            if (RecordSize > 256) return 2;
            CheckSum = RecordSize;

            Offset =  AsciiToByte(file[i]) * 4096;
            CheckSum += AsciiToByte(file[i]) * 16;
            i++;
            Offset += AsciiToByte(file[i]) * 256;
            CheckSum += AsciiToByte(file[i]);
            i++;
            Offset += AsciiToByte(file[i]) * 16;
            CheckSum += AsciiToByte(file[i]) * 16;
            i++;
            Offset += AsciiToByte(file[i]);
            CheckSum += AsciiToByte(file[i]);
            i++;
            if ((Offset + RecordSize) >= MaxPoint) return 3;
            else BytePointer = Offset;
            if((Offset + RecordSize - 1) > LastBytePoint) LastBytePoint = Offset + RecordSize -1;

            if (file[i] != '0') return 4;
            i++;
            if (file[i] == '1')
            {
                if (RecordSize != 0) return 9;
                i++;
                if (file[i] != 'F') return 5;
                i++;
                if (file[i] != 'F') return 5;
                i++;
                if (file[i] != 13) return 7;
                i++;
                if (file[i] != 10) return 8;
                EndOfFile = LastBytePoint;
                break;
            }
            else if (file[i] != '0') return 4;
            i++;
            for(int j=0; j<RecordSize; j++)
            {
                Byte[BytePointer] = (byte)(AsciiToByte(file[i]) * 16);
                i++;
                Byte[BytePointer] += (byte)(AsciiToByte(file[i]));
                CheckSum += Byte[BytePointer];
                BytePointer++;
                i++;
            }
            CheckSumM = (AsciiToByte(file[i]) * 16);
            i++;
            CheckSumM += AsciiToByte(file[i]);
            i++;
            CheckSumT = ((~CheckSum) & 0xFF) + 1;
            if (CheckSumM != CheckSumT) return 6;


            if (file[i] != 13) return 7;
            i++;
            if (file[i] != 10) return 8;
            i++;

            CurrentRecord++;

        }
        ByteArr = Byte;
        return 0;
    }

its that for now and works fine . thanks everyone