Solved

LRC Check of Byte Array in C#

Posted on 2013-06-19
6
2,868 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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