Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I need to decode the following code i.e use of code

public static string CalcChecksum(string instrFile)
    {
      ulong num1 = 0;
      uint num2 = 256;
      Tools.ReplaceChecksum(ref instrFile, "0000000000");
      byte[] bytes = Encoding.GetEncoding(1252).GetBytes(instrFile);
      int index = 0;
      while (index <= instrFile.Length - 1)
      {
        byte num3 = bytes[index];
        byte num4;
        byte num5;
        byte num6;
        if (index + 1 < instrFile.Length)
        {
          num4 = bytes[index + 1];
          if (index + 2 < instrFile.Length)
          {
            num5 = bytes[index + 2];
            num6 = index + 3 >= instrFile.Length ? (byte) 0 : bytes[index + 3];
          }
          else
          {
            num5 = (byte) 0;
            num6 = (byte) 0;
          }
        }
        else
        {
          num4 = (byte) 0;
          num5 = (byte) 0;
          num6 = (byte) 0;
        }
        ulong num7 = (ulong) (num2 * (num2 * (num2 * (uint) num3 + (uint) num4) + (uint) num5) + (uint) num6);
        num1 += num7;
        if (num1 > 4294967296UL)
          num1 -= 4294967296UL;
        index += 4;
      }
      return num1.ToString("0000000000");
    }


What I have tried:

I have tried and get an output numeral string corresponding to input string to function.
Posted
Updated 3-Sep-17 20:49pm
Comments
Graeme_Grant 4-Sep-17 2:09am    
Decode how? And for what purpose? It is a mathematically calculated value.
Patrice T 4-Sep-17 2:38am    
There is not such thing as decoding a checksum.

1 solution

The problem is that there are several different forms of checksum, and we have no idea what type you are trying to calculate, or what type you should be using. And they range from the trial "summ and the bytes and throw away and carry" to SHA hashes!

As a result we can't tell you what to do to fix your method - so, it's going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900