Solved

LRC Check of Byte Array in C#

Posted on 2013-06-19
6
3,064 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction Many of the most common information processing tasks require sorting data sets.  For example, you may want to find the largest or smallest value in a collection.  Or you may want to order the data set in numeric or alphabetical order. …
Introduction This question got me thinking... (http://www.experts-exchange.com/questions/28707487/GLOBALS.html) Why shouldn't we use Globals? This is a simple question without a simple answer.  How do you explain these concepts to a programmer w…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

729 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