// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public static class Crc8 { static byte[] table = new byte[256]; // x8 + x7 + x6 + x4 + x2 + 1 const byte poly = 0xd5; public static byte ComputeChecksum(params byte[] bytes ) { byte crc = 0; if ( bytes != null && bytes.Length > 0 ) { foreach ( byte b in bytes ) { crc = table[crc ^ b]; } } return crc; } static Crc8( ) { for ( int i = 0; i < 256; ++i ) { int temp = i; for ( int j = 0; j < 8; ++j ) { if ( ( temp & 0x80 ) != 0 ) { temp = ( temp << 1 ) ^ poly; } else { temp <<= 1; } } table[i] = (byte)temp; } } } public class Program { public static void Main(string[] args) { byte[] b = { 1, 2, 3, 4 }; Console.WriteLine(Crc8.ComputeChecksum(b)); } } }