I've been researching this everywhere and all the LRC implementation seems to not giving me the right answer. After spending few days on it, I decided to put my code here to see if anyone else can spot the problem.
Here's the code (C#)
//Input Data = "31303030315E315E31303030325E315E31303030375E39395E31303032325E36353631335E"
//LRC Answer = "30"
private static string LRC(string Data)
{
int checksum = 0;
foreach (char c in GetStringFromHex(Data))
{
checksum ^= Convert.ToByte(c);
}
string hex = checksum.ToString("X2");
Console.WriteLine("Calculated LRC = " + hex);
return hex;
}
//Supporting Function used in LRC function
private static string GetStringFromHex(string s)
{
string result = "";
string s2 = s.Replace(" ", "");
for (int i = 0; i < s2.Length; i += 2)
{
result += Convert.ToChar(int.Parse(s2.Substring(i, 2), System.Globalization.NumberStyles.HexNumber));
}
return result;
}
The current output shows "Calculated LRC = 33". However, the right answer is "30". Can anyone spot what's wrong with this?
Any help will be fantastic!