Solved

LRC Check of Byte Array in C#

Posted on 2013-06-19
6
2,811 Views
Last Modified: 2013-06-20
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;
        }

Open in new window


Can anyone confirm that this is proper?

Thanks,

J
0
Comment
Question by:BullfrogSoftware
  • 2
  • 2
  • 2
6 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39259324
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;
    }

Open in new window

0
 
LVL 1

Author Comment

by:BullfrogSoftware
ID: 39259338
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
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39259380
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:BullfrogSoftware
ID: 39259387
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
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 250 total points
ID: 39259388
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
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 250 total points
ID: 39259401
Yes. Yours is correct according to that requirement.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now