AnsweredAssumed Answered

CRC32 calculation in C#

Question asked by luketrb on Aug 4, 2014
Latest reply on Feb 14, 2018 by kristian faulkner
Hi,

I created a C# class which calculates the CRC32 checksum exactly like the STM32. 

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;
        }
    }
}

Outcomes