本文介绍了C#计算LRC(纵向冗余校验)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在研究这个无处不在,所有的LRC实施似乎没有给我正确的答案。在花费几天后,我决定把我的code这里看看是否有人能发现问题。
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.
这里的code(C#)
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;
}
目前的输出显示计算LRC = 33。然而,正确的答案是30。任何人都可以发现有什么不对呢?
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!
推荐答案
经过多次测试,确认LRC应包括ETX和LRC计算时不包括STX。
After several testing, it is confirmed LRC should include ETX and exclude STX during the LRC calculation.
这篇关于C#计算LRC(纵向冗余校验)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!