views:

52

answers:

2

I'm trying to figure out how to check a checksum. My message looks like this:

38 0A 01 12 78 96 FE 00 F0 FB D0 FE F6

F6 being the checksum. I convert the preceding 12 sets in to binary and then add them together. Then attempt a bitwise operation to apply the 2s complement. I get a value of -1562, but I can't convert it back to hex to check if the value is correct. Can someone point me in the right direction?

my code:

string[] hexValue = {"38", "0A", "01", "12", "78", "96", "FE", "00", "F0", "FB", "D0", "FE"};
int totalValue = 0;
foreach(string item in hexValue)
{
    totalValue += Int32.Parse(item, NumberStyles.HexNumber);
}

int bAfter2sC = ~totalValue + 1;
Console.Write("answer :" + bAfter2sC + "\n");
+1  A: 

Coverting to hex is available using ToString: bAfter2sC.ToString("X8")

Stephen Cleary
A: 

I'm not sure what algorithm you are using (-1562 requires 3 bytes, not two), but how about using a standard check digit algorithm, like mod 11, also known as ISDN 10 as documented here: "it can be validated very simply by adding all the products together then dividing by 11."

ShellShock
-1562 fits into a two-byte signed integer...
BlueRaja - Danny Pflughoeft