liv2hak
liv2hak

Reputation: 14970

8-bit modulo 256 sum CRC

I have a class for my protocol header in C#

public class Header
{
    public UInt16 m_syncBytes;
    public UInt16 m_DestAddress;
    public UInt16 m_SourceAddress;

    public byte m_Protocol;
    public byte m_SequnceNumber;

    public byte m_Length;
    public byte m_HdrCrc;
}

I want to calculate an 8-bit modulo 256 sum of header block characters from m_DestAddress to m_Length

I have come across a lot of examples for 16 bit CRC's online.But couldn't find 8 bit Modulo 256 sum CRC. It would be great if someone could explain how to calculate that.

Upvotes: 1

Views: 3169

Answers (1)

itsme86
itsme86

Reputation: 19486

This is the way I'd do it:

public byte GetCRC()
{
    int crc;  // 32-bits is more than enough to hold the sum and int will make it easier to math
    crc = (m_DestAddress & 0xFF) + (m_DestAddress >> 8);
    crc += (m_SourceAddress & 0xFF) + (m_SourceAddress >> 8);
    crc += m_Protocol;
    crc += m_SequenceNumber;
    crc += m_Length;

    return (byte)(crc % 256);  // Could also just do return (byte)(crc & 0xFF);
}

Upvotes: 2

Related Questions