# LRC Check of Byte Array in C#

I am using this:

``````private byte GetLRC(byte[] bArray, int start, int end)
{
byte LRC = 0;

for (int i = start; i <= end; i++)
{
LRC = (byte)(LRC ^ bArray[i]);
}

return LRC;
}
``````

Can anyone confirm that this is proper?

Thanks,

J
LVL 1
###### Who is Participating?

Commented:
Yes. Yours is correct according to that requirement.
0

Full stack Software EngineerCommented:
u don't need start and end cause the array has property length:
try this code (from wikipedia):
``````private byte calculateLRC(byte[] b)
{
byte lrc = 0x00;
for (int i = 0; i < b.Length; i++)
{
lrc = (byte)((lrc + b[i]) & 0xFF);
}
lrc = (byte)(((lrc ^ 0xff) + 2) & 0xFF);
return lrc;
}
``````
0

Author Commented:
I saw this code, and was going to post it along with my question.  It produces a different result.  So, I was wondering how I could 'hand calculate' to check where I am going wrong.

Also, I have start and end because in my case the arrays in question will have an empty slot at the end for the LRC, so in that case, I check up to the end -1, and then post the result in the empty slot.

J
0

Commented:
Also from that same article "The term usually applies to a single parity bit per bit stream ... although it could also be used to refer to a larger Hamming code"

So clearly there isn't one single right way to do it.

LRC (longitudinal redundancy check) is a specific idea of checking, not a specific algorithm for how to check. Your algorithm does fit the definition of an LRC. You append one byte that you calculate across all the data so you can check if two streams are the same. A simple XOR is a reasonable check. If you are concerned about latency or just like the simplicity, it's a good algorithm.
0

Author Commented:
Yes, the software requirements call for an LRC that is the Xor of all bytes in the array.  So this is correct according to that requirement?

Jeremy
0

Full stack Software EngineerCommented:
the wikipedia example is wrong and a fix was posted on stackoverflow:
http://stackoverflow.com/questions/12799122/how-can-i-calculate-longitudinal-redundancy-check-lrc
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.