Question
CRC32 calculation in C#
Posted on August 04, 2014 at 10:47
Hi,
I created a C# class which calculates the CRC32 checksum exactly like the STM This may be useful to others:using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Security.Cryptography;
namespace
JCC224_User_Interface
{
public
static
class
CRC32
{
public
static
UInt32 calculateWord(UInt32 crc, UInt32 data)
{
int
i;
crc = crc ^ data;
for
(i = 0; i < 32; i++)
{
if
((crc & 0x80000000) != 0)
{
crc = (crc << 1) ^ 0x04C11DB7;
// Polynomial used in STM32
}
else
{
crc = (crc << 1);
}
}
return
(crc);
}
public
static
UInt32 calculateBuffer(UInt32 initial_value, UInt32[] buffer)
{
for
(
int
i = 0; i < buffer.Length; i++)
{
initial_value = calculateWord(initial_value, buffer[i]);
}
return
initial_value;
}
}
}
#crc32