Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

LRC Check of Byte Array in C#

Posted on 2013-06-19
6
Medium Priority
?
3,227 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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 1000 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 1000 total points
ID: 39259401
Yes. Yours is correct according to that requirement.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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