138-c-sharp-crc-checksum-code-for-binary-files

A cyclic redundancy check (CRC) is an error-detecting code. In this article, C# crc checksum code for binary files is given. For 16 bit CRC calculus for binary files, first step is to have all of the data (all of the bytes) stored in byte array. For accessing binary file, System.IO namespace is necessary. Then, we need FileStream object to open binary file.

FileStream fsBinaryRead = new FileStream(openFileDialog1.FileName, FileMode.Open);

Next important information to be obtained is the size of data to be read. This information can be stored in variable of var type.
var var_data_size = (int)fsBinaryRead.Length;

byte[] bytData = new byte[var_data_size];
byte[] bytOutputData = new byte[var_data_size + 2];
fsBinaryRead.Read(bytData, 0, var_data_size);

Output data array has to be for two items bigger then the input array (for storing 16 CRC bits). Next step is to implement crc checksum code.

int int_crc = 0xFFFF;
int int_lsb;
int int_crc_byte_a, int_crc_byte_b;
byte byt_a, byt_b;

for (int i = 0; i < bytData.Length; i++)
{

bytOutputData[i] = bytData[i];

}
for (int i = 0; i < bytData.Length; i++)
{

int_crc = int_crc ^ bytOutputData[i];
for (int j = 0; j < 8; j++)
{

int_lsb = int_crc & 0x0001; // Mask of LSB
int_crc = int_crc >> 1;
int_crc = int_crc & 0x7FFF;
if (int_lsb == 1) int_crc = int_crc ^ 0xA001;

}

}
int_crc_byte_a = int_crc & 0x00FF;
int_crc_byte_b = (int_crc >> 8) & 0x00FF;
byt_a = (byte)int_crc_byte_a;
byt_b = (byte)int_crc_byte_b;
bytOutputData[var_data_size] = byt_a;
bytOutputData[var_data_size + 1] = byt_b;

After CRC calculus is done, and CRC bytes are added, FileStream object has to be closed.

fsBinaryRead.Close();

As the final step, we need to open binary file and save the data.

FileStream fsBinaryWrite = File.Create(openFileDialog1.FileName);
fsBinaryWrite.Write(bytOutputData, 0, bytOutputData.Length);
fsBinaryWrite.Close();

Whole code is as follows:

private void cmdOpenSave_Click(object sender, EventArgs e)
{

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{

FileStream fsBinaryRead = new FileStream(openFileDialog1.FileName, FileMode.Open);
var var_data_size = (int)fsBinaryRead.Length;
byte[] bytData = new byte[var_data_size];
byte[] bytOutputData = new byte[var_data_size + 2];
fsBinaryRead.Read(bytData, 0, var_data_size);

int int_crc = 0xFFFF;
int int_lsb;
int int_crc_byte_a, int_crc_byte_b;
byte byt_a, byt_b;

for (int i = 0; i < bytData.Length; i++)
{

bytOutputData[i] = bytData[i];

}
for (int i = 0; i < bytData.Length; i++)
{

int_crc = int_crc ^ bytOutputData[i];
for (int j = 0; j < 8; j++)
{

int_lsb = int_crc & 0x0001; // Mask of LSB
int_crc = int_crc >> 1;
int_crc = int_crc & 0x7FFF;
if (int_lsb == 1) int_crc = int_crc ^ 0xA001;

}

}
int_crc_byte_a = int_crc & 0x00FF;
int_crc_byte_b = (int_crc >> 8) & 0x00FF;
byt_a = (byte)int_crc_byte_a;
byt_b = (byte)int_crc_byte_b;
bytOutputData[var_data_size] = byt_a;
bytOutputData[var_data_size + 1] = byt_b;

fsBinaryRead.Close();

FileStream fsBinaryWrite = File.Create(openFileDialog1.FileName);
fsBinaryWrite.Write(bytOutputData, 0, bytOutputData.Length);
fsBinaryWrite.Close();

}

}

External links:

C# CRC Checksum for Binary Files on Codeguru
C# CRC Checksum standard CRC16 on Sanity-Free
C# CRC Checksum CRC16 CCITT on Sanity-Free

Posted in c-sharp-code-examples

Leave a Reply

Your email address will not be published. Required fields are marked *

*