8 位模 256 CRC
8-bit modulo 256 sum CRC
我的协议 header 在 C#
中有一个 class
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;
}
我想计算从 m_DestAddress
到 m_Length
的 header 块字符的 8 位模 256 和
我遇到过很多 16 位 CRC 的示例 online.But 找不到 8 位模 256 和 CRC。如果有人能解释一下如何计算就太好了。
我会这样做:
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);
}
我的协议 header 在 C#
中有一个 classpublic 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;
}
我想计算从 m_DestAddress
到 m_Length
我遇到过很多 16 位 CRC 的示例 online.But 找不到 8 位模 256 和 CRC。如果有人能解释一下如何计算就太好了。
我会这样做:
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);
}