Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I calculate Longitudinal Redundancy Check (LRC)?

I've tried the example from wikipedia: http://en.wikipedia.org/wiki/Longitudinal_redundancy_check

This is the code for lrc (C#):

/// <summary>
/// Longitudinal Redundancy Check (LRC) calculator for a byte array. 
/// ex) DATA (hex 6 bytes): 02 30 30 31 23 03
///     LRC  (hex 1 byte ): EC    
/// </summary> 
public static byte calculateLRC(byte[] bytes)
{
    byte LRC = 0x00;
    for (int i = 0; i < bytes.Length; i++)
    {
        LRC = (LRC + bytes[i]) & 0xFF; 
    }
    return ((LRC ^ 0xFF) + 1) & 0xFF;
}   

It said the result is "EC" but I get "71", what I'm doing wrong?

Thanks.

like image 802
Alexx Perez Avatar asked Oct 09 '12 11:10

Alexx Perez


People also ask

How do you calculate LRC?

The LRC is calculated by adding together successive eight-bit bytes in the message, discarding any carries, then two's complementing the result.

How do you do a longitudinal redundancy check?

Longitudinal Redundancy Check (LRC)/2-D Parity CheckA block of bit is divided into table or matrix of rows and columns. In order to detect an error, a redundant bit is added to the whole block and this block is transmitted to receiver. The receiver uses this redundant row to detect error.

How many errors can LRC detect?

VRC can detect single bit errors. LRC can detect burst errors. 4.

What is the full form of LRC?

LRC (Longitudinal Redundancy Check)


1 Answers

Here's a cleaned up version that doesn't do all those useless operations (instead of discarding the high bits every time, they're discarded all at once in the end), and it gives the result you observed. This is the version that uses addition, but that has a negation at the end - might as well subtract and skip the negation. That's a valid transformation even in the case of overflow.

public static byte calculateLRC(byte[] bytes)
{
    int LRC = 0;
    for (int i = 0; i < bytes.Length; i++)
    {
        LRC -= bytes[i];
    }
    return (byte)LRC;
}

Here's the alternative LRC (a simple xor of bytes)

public static byte calculateLRC(byte[] bytes)
{
    byte LRC = 0;
    for (int i = 0; i < bytes.Length; i++)
    {
        LRC ^= bytes[i];
    }
    return LRC;
}

And Wikipedia is simply wrong in this case, both in the code (doesn't compile) and in the expected result.

like image 145
harold Avatar answered Sep 18 '22 12:09

harold