0

I have to extract the LRC From : "02 02 30 30 30 30 30 30 30 30 30 30 30 31 31 30 30 1c 35 32 33 1c 1c 2b 30 1c 39 34 36 1c 1c 1c 1c 1c 1c 1c 1c 1c 1c 1c 30 30 1c 1c 1c 1c 1c 1c 03" the corest answer must be : " 3B "

I tested the codes below but the result is wrong .

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

    private static byte calculateLRC2(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;
    }

    public static char CalculateLRC3(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);
    }

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

I'm waiting for your answer, thank you!!

1 Answers1

0

Looking at the Wikipedia article for LRC, it says the pseudocode for LRC is

Set LRC = 0
For each byte b in the buffer
do
    Set LRC = (LRC + b) AND 0xFF
end do
Set LRC = (((LRC XOR 0xFF) + 1) AND 0xFF)

This seems similar to your calculateLRC2 function, however you're doing + 2 instead of the + 1 prescribed above. Might that be the problem?

AKX
  • 152,115
  • 15
  • 115
  • 172