private byte CalculateChecksum(byte[] realbytesend)
{
int checksum = 0;
for (int i = 0; i < realbytesend.Length ; i++)
{
string strchecksum = realbytesend.GetValue(i).ToString();
int intchecksum = Convert.ToInt32(strchecksum);
checksum += intchecksum;
}
string csumbit = checksum.ToString("X"); //Gives you hexadecimal
string checksumbit = string.Format("0x{0}", csumbit);
style = NumberStyles.HexNumber;
bytechecksum = Byte.Parse(checksumbit, style);
return bytechecksum;
}
when i debug this code, there is an error message with ' System.FormatException:"The input string is malformed." ' i dont know why it happen.
i already tried convert.ToByte and byte.parse(string), but i dont know why it doesnt work!!
i want you to help me...
checksumvariable is an integer, and very likely greater than 0xff (the max value for a byte). What do you expect Byte.Parse(checksumbit, style) to do with a number that size? What is the point of all the conversions to and from a string? Why not calculate a sum (or do some XOR operations) and then mask the result with0xff