[C#] Binary 8-bit checksum?

Discussion in 'Mixed Languages' started by FreeStyler, Nov 22, 2010.

  1. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,557
    3,832
    120
    #1 FreeStyler, Nov 22, 2010
    Last edited by a moderator: Apr 20, 2017
    How to calculate 8-bit checksum of a SLIC bin file?

    I am reading the file like:
    Code:
    byte[] slic = File.ReadAllBytes(filename);
    
     
  2. BobSheep

    BobSheep MDL Guru

    Apr 19, 2010
    2,330
    1,377
    90
    #2 BobSheep, Nov 22, 2010
    Last edited by a moderator: Apr 20, 2017
    Code:
                string file = "slic.bin";
    
                BinaryReader br = new BinaryReader(File.Open(file, FileMode.Open));
    
                byte[] bytes = br.ReadBytes((int)br.BaseStream.Length);
                br.Close();
    
                bytes[9] = 0; // reset checksum to 0
    
                byte cs = 0;
    
                for (int i = 0; i < bytes.Length; i++)
                {
                    cs += bytes;
                }
    
                cs *= 0XFF; // new checksum
     
    
     
  3. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,557
    3,832
    120
    #3 FreeStyler, Nov 22, 2010
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Code:
        private byte checksum(String filename)
        {
            BinaryReader br = new BinaryReader(File.Open(filename, FileMode.Open));
    
            byte[] bytes = br.ReadBytes((int)br.BaseStream.Length);
            br.Close();
    
            bytes[9] = 0; // reset checksum to 0
    
            byte cs = 0;
    
            for (int i = 0; i < bytes.Length; i++)
            {
                cs += bytes;
            }
    
            return cs *= 0XFF; // new checksum
        }
    


    Not exactly sure what this should return? i just need it to report the current 8-bit checksum of the file.
    It returns a value different as the one reported by winhex, eg: winhex return a 2 character value, this returns 3 characters
     
  4. BobSheep

    BobSheep MDL Guru

    Apr 19, 2010
    2,330
    1,377
    90
    Ok. I misunderstood. I thought you wanted to calculate the SLIC checksum.
    If you want to find the existing checksum regardless of if it's correct or not then byte[9] is the value you need.

    This should return a 2 hex char value which will be displayed as 0X??

    This is the routine I use to check if the SLICs that get reported have been trashed or not so I know it works.
     
  5. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,557
    3,832
    120
    #5 FreeStyler, Nov 22, 2010
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Yes, it just needs to report the SLIC's current 8-bit checksum (I am rebuilding http://d-fault.nl/test/ in asp.net, so it only needs to report the checksum value)

    EDIT, got it working, it now displays it's current value

    Code:
        private string checksum(String filename)
        {
            BinaryReader br = new BinaryReader(File.Open(filename, FileMode.Open));
    
            byte[] bytes = br.ReadBytes((int)br.BaseStream.Length);
            br.Close();
    
            byte cs = 0;
    
            for (int i = 0; i < bytes.Length; i++)
            {
                cs += bytes;
            }
            return string.Format("{0:X2}", cs);
        }
    



    [​IMG] [​IMG]

    Most important features:
    • Automatically corrects SLIC checksum
    • Reports SLIC validity
    • Reports SLIC version

    * Requires .Net framework 3.5

    http://forums.mydigitallife.net/attachment.php?attachmentid=7686&d=1290521722

    Kudos to BobSheep ;)