Hex checksum

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jarhead
    New Member
    • Jan 2010
    • 3

    Hex checksum

    Hey all,
    I'm completely new to programming of any kind and I decided to try and write a driver for a XM tuner.
    I'm trying to figure out how to send a hex array which has 4 bytes that will change with each command. The 4 bytes will be tuner number (appears twice), the actual event wanting to happen, and the checksum.
    To start I want to just figure out how to calculate the checksum and take it from there.
    This is the formula:
    Step 1: Add byte values: 0xF0 + 0x00 + 0x67 + 0x7C + 0xF1 + 0x0F = 0x2D3
    Step 2: Add byte count: 0x2D3 + 6 (Decimal value byte count) = 0x2D9
    Step 3: Bitwise AND with 0x7F: 0x02D9 & 0x007F = 0x59 for the Checksum

    Any ideas?
    Thanks
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Hey check this out to see if it will help you:

    Comment

    • Jarhead
      New Member
      • Jan 2010
      • 3

      #3
      Anyone else have any ideas, or preferably, some examples?

      Comment

      • dennisoleksyuk
        New Member
        • Jan 2010
        • 17

        #4
        You posted question in C# thread.
        Here is C# implementation of your three steps.
        Code:
                    byte[] byteArray = { 0xF0, 0x00, 0x67, 0x7C, 0xF1, 0x0F };
        
                    //Variable with result of your calculation.
                    int result = 0;
        
                    //Step1: Add byte values.            
                    foreach (byte value in byteArray) {
                        result += value;
                    }
        
                    //Step2: Add byte count.
                    result += 6;
        
                    //Step3: Bitwise AND with 0x7F.
                    result &= 0x7F;

        Comment

        • Jarhead
          New Member
          • Jan 2010
          • 3

          #5
          Almost...
          It's just not returning the correct checksum.

          If you try this:

          Code:
          byte[] byteArray = { 0xF0, 0x00, 0x7D, 0x00, 0x00, 0x00, 0x70, 0x05, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x3B, 0x00, 0x70, 0x00, 0x00, 0x00, 0x01 };
          And change the byte count to 21, it should return 29 but it returns 41?

          Comment

          • dennisoleksyuk
            New Member
            • Jan 2010
            • 17

            #6
            Decimal 41 is hex 29. Console.WriteLi ne print all numbers as decimal

            Comment

            Working...