Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CRC Calculator Implementation in VB.NET

Posted on 2007-11-26
7
Medium Priority
?
5,221 Views
Last Modified: 2012-08-14
So I kind of need an CRC check in my code that i have written (I am reading stuff from serial).  I want to be able to send an integer (or even better a byte() ), and then return the 16-CRC for that.  I kind of have a vague recollection of how CRC works, but from whatI remember I have to break things down by bits.  Is that even possible in .NET?  Please provide me with as much info on doing this task as possible.  If there is a class already implemented somewhere online (open source of course), that would work as well.  It has to be something that I can integrate with my code though.
0
Comment
Question by:feldmani
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:Thejaka
ID: 20355348
There are various crc algorithms. For information regarding some of these, try http://en.wikipedia.org/wiki/Cyclic_redundancy_check

For an implementation in C#, try http://www.codeproject.com/csharp/marcelcrcencoding.asp

I did a particular implementation in C# sometime back, but first, you have to pick your algorithm!
0
 
LVL 1

Author Comment

by:feldmani
ID: 20355523
Unfortunately I need VB.NET, not C#.  I know I need CRC16 since that is what the motes are using.  I already looked through the wiki for this, didn't find anything that immediately looked helpful.
0
 
LVL 1

Author Comment

by:feldmani
ID: 20355526
I see what you are saying about the CRC16 having different versions.  Let me calculate and see which one I have.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 1

Author Comment

by:feldmani
ID: 20411932
CRC-16-CCITT Is the type I need.  Sorry for the delay in the response.
0
 
LVL 8

Expert Comment

by:Thejaka
ID: 20416923
I don't believe I have the code of an implementation of that algorithm with me at the moment, but there are several existing implementations publicly  available on the web.

Below is the implementation of the algorithm found at http://www.sanity-free.org/133/crc_16_ccitt_in_csharp.html converted from C# to VB.NET.

I guess you can use it as a starting point and adapt it to your needs.

NOTE: I haven't taken the time to verify the operation of the original implementation, simply translated it to VB.NET. Anyhow, if you have any trouble with this, let me know.

Also, depending on your requirements, it may be possible to implement the algorithm as a single function.
0
 
LVL 8

Accepted Solution

by:
Thejaka earned 2000 total points
ID: 20416960
Original: http://www.sanity-free.org/133/crc_16_ccitt_in_csharp.html

Translated to VB.NET.

Imports System
 
Public Enum InitialCrcValue
  Zeros
  NonZero1 = 65535
  NonZero2 = 7439
End Enum
 
Public Class Crc16Ccitt
  Const poly As UShort = 4129
  Private table As UShort() = New UShort(255) {}
  Private initialValue As UShort = 0
 
  Public Function ComputeChecksum(ByVal bytes As Byte()) As UShort
    Dim crc As UShort = Me.initialValue
    For i As Integer = 0 To bytes.Length - 1
      crc = CUShort(((crc << 8) Xor table(((crc >> 8) Xor (255 And bytes(i))))))
    Next
    Return crc
  End Function
 
  Public Function ComputeChecksumBytes(ByVal bytes As Byte()) As Byte()
    Dim crc As UShort = ComputeChecksum(bytes)
    Return New Byte() {CByte((crc >> 8)), CByte((crc And 255))}
  End Function
 
  Public Sub New(ByVal initialValue As InitialCrcValue)
    Me.initialValue = CUShort(initialValue)
    Dim temp As UShort, a As UShort
    For i As Integer = 0 To table.Length - 1
      temp = 0
      a = CUShort((i << 8))
      For j As Integer = 0 To 7
        If ((temp Xor a) And 32768) <> 0 Then
          temp = CUShort(((temp << 1) Xor poly))
        Else
          temp <<= 1
        End If
        a <<= 1
      Next
      table(i) = temp
    Next
  End Sub
End Class

Open in new window

0
 
LVL 1

Author Comment

by:feldmani
ID: 20422783
Thanks,  Let me try playing with this and see if I am getting the right answer.  Greatly appreciated.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question