.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

checksum code in c#

Posted By: Sievy     Posted Date: July 28, 2014    Points:5   Category :C#
I need to rewrite the code below in c#.
I think the code is written in c but I only understand a few lines.
Can someone help me explain the code?

byte crc8(byte *str, int len)
{
int i, f;
byte data;
byte crc;
crc = 0 ;

while (len--)
{
data = *str++;
for = (i = 0; i < 8; i++)
{
f = 1 & (data ^ crc);
crc >>= 1;
data >>= 1 ;
if (f)
{
crc ^= 0x8c;
}
}
}
return crc;
}

Responses
Author: satyapriyanayak             
Posted Date: July 29, 2014     Points: 20   

http://www.c-sharpcorner.com/Forums/Thread/262992/checksum-code-in-C-Sharp.aspx

If this post helps you mark it as answer
Thanks
Author: ASPEvil             
Posted Date: July 30, 2014     Points: 20   

Hello,

"crc" stands for Cyclic Redundancy Check. CRC 8 is a algorithm for doing Cyclic Redundancy Check.

If you want to know more about "crc 8", then you can read the contents of the following link:-
-----------------------------------------
http://en.wikipedia.org/wiki/Computation_of_cyclic_redundancy_checks
-----------------------------------------

And if you want to use the crc 8 algorithm with the c#, then you can check the following urls:-
--------------------------
http://www.sanity-free.com/146/crc8_implementation_in_csharp.html

http://www.dotnetfunda.com/articles/show/2531/how-to-calculate-crc-8-check-sum-using-csharp
--------------------------


Thanks.

Author: Sambanthamoorthy             
Posted Date: September 02, 2014     Points: 20   

Yes, it looks like C code with a typedef for byte as unsigned char would normally be used for that particular type.

Anyway, this C program works fine and outputs 22:

#include <stdio.h>
#include <string.h>

typedef unsigned char byte;

byte crc8(byte *str, int len)
{
int i, f;
byte data;
byte crc;
crc = 0 ;
while (len--)
{
data = *str++;
for(i = 0; i < 8; i++)
{
f = 1 & (data ^ crc);
crc >>= 1;
data >>= 1;
if (f)
{
crc ^= 0x8c;
}
}
}
return crc;
}

int main()
{
byte *str = "The quick brown fox jumps over the lazy dog";
size_t len = strlen(str);
byte crc = crc8(str, len);
printf("%d", crc);
return 0;
}

You can actually convert this to C# with very little change if you use 'unsafe' code and compile with the /unsafe switch:

using System;
using System.Text;

unsafe class Program
{
static byte crc8(byte *str, int len)
{
int i, f;
byte data;
byte crc;
crc = 0 ;
while (len-- != 0)
{
data = *str++;
for(i = 0; i < 8; i++)
{
f = 1 & (data ^ crc);
crc >>= 1;
data >>= 1;
if (f != 0)
{
crc ^= 0x8c;
}
}
}
return crc;
}

static void Main()
{
string str = "The quick brown fox jumps over the lazy dog";
byte[] bytes = Encoding.ASCII.GetBytes(str);
int len = bytes.Length;
fixed(byte* pb = bytes)
{
byte crc = crc8(pb, len);
Console.WriteLine(crc);
}
}
}

This again outputs 22.

However, if you'd prefer to use 'safe' code, you can use this instead - execution speed will probably be a little slower:

using System;
using System.Text;

class Program
{
static byte crc8(byte[] str) // no need to pass length in 'safe' code
{
int len = str.Length;
int i, f;
byte data;
byte crc;
crc = 0 ;
int j = 0;

while (len-- != 0)
{
data = str[j++];
for(i = 0; i < 8; i++)
{
f = 1 & (data ^ crc);
crc >>= 1;
data >>= 1;
if (f != 0)
{
crc ^= 0x8c;
}
}
}

return crc;
}

static void Main()
{
string s = "The quick brown fox jumps over the lazy dog";
byte[] str = Encoding.ASCII.GetBytes(s);
byte crc = crc8(str);
Console.WriteLine(crc);
}
}

The programs are of course computing a 'cyclic redundancy check' which is often used for checksums and the like.

If you need to understand the mathematics of how this works, I'd check out this Wikipedia article and follow the associated links:



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend