How can I calculate Longitudinal Redundancy Check (LRC)?

37,438

Solution 1

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.

Solution 2

Guess this one looks cooler ;)

public static byte calculateLRC(byte[] bytes)
{
    return bytes.Aggregate<byte, byte>(0, (x, y) => (byte) (x^ y));
}

Solution 3

If someone wants to get the LRC char from a string:

    public static char CalculateLRC(string toEncode)
    {
        byte[] bytes = Encoding.ASCII.GetBytes(toEncode);
        byte LRC = 0;
        for (int i = 0; i < bytes.Length; i++)
        {
            LRC ^= bytes[i];
        }
        return Convert.ToChar(LRC);
    }
Share:
37,438
Alexx Perez
Author by

Alexx Perez

Updated on May 08, 2020

Comments

  • Alexx Perez
    Alexx Perez about 4 years

    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.