Results 1 to 11 of 11

Thread: CRC16 generation

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Location
    Huddersfield, England
    Posts
    83

    CRC16 generation

    I've wanting to generate a CRC16-X25 checksum in the form HiByte and LowByte.

    All the examples I've found by searching are either VB6 which uses functions and data types that are no longer supported or C, C++ etc.

    Does anyone have a simple VB.Net version? Speed is not an issue.

  2. #2

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: CRC16 generation

    Post the VB6 version. Ill help you convert it if I have time.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Location
    Huddersfield, England
    Posts
    83

    Re: CRC16 generation

    This is a C example with a VB conversion, but the solution is using non vb functions:

    http://forums.codeguru.com/showthread.php?t=475191

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: CRC16 generation

    This is what they have there:-
    vbnet Code:
    1. '
    2. Private Function CRC16(ByVal crc As Integer, d As Byte) As Integer
    3.   Dim carry As Integer, i As Byte
    4.   For i = 0 To 7
    5.     carry = (crc And 1) Xor IIf(d And (2 ^ i), 1, 0)
    6.     crc = (crc And &HFFFF&) \ 2
    7.     If carry <> 0 Then crc = crc Xor &H8408
    8.   Next i
    9.   CRC16 = crc
    10. End Function
    11.  
    12. Public Function CalcCrc16(ByVal buf As String) As String
    13.   Dim crc As Integer, t As Integer
    14.   crc = &H8408
    15.   For t = 1 To Len(buf)
    16.     crc = CRC16(crc, Asc(Mid$(buf, t, 1)))
    17.   Next t
    18.   CalcCrc16 = Chr$(HiByte(crc)) & Chr$(LoByte(crc))
    19. End Function
    What functions do you see that dont belong to VB ? Are you referring to the HiByte/LoByte functions ? Even so this is rather trivial to convert to VB.Net do you need help to convert it or do you think you can manage ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: CRC16 generation

    Here, I made two functions so you can extract the high and low bytes should you still want to convert that VB6 one:-
    vbnet Code:
    1. '
    2.     Private Declare Function RtlMoveMemory Lib "Kernel32" (ByRef dest As Byte, ByRef src As Short, ByVal cb As Integer) As Integer
    3.  
    4.     Private Function HiByte(ByVal num As Short) As Byte
    5.  
    6.         Dim arNum(1) As Byte
    7.  
    8.         RtlMoveMemory(arNum(0), num, 2)
    9.  
    10.         Return arNum(1)
    11.  
    12.     End Function
    13.  
    14.  
    15.     Private Function LoByte(ByVal num As Short) As Byte
    16.  
    17.         Dim arNum(1) As Byte
    18.  
    19.         RtlMoveMemory(arNum(0), num, 2)
    20.  
    21.         Return arNum(0)
    22.  
    23.     End Function
    Also if you are going to convert from VB6, remember to change all those Integers to Shorts. You could also opt to use an online converter to convert the C# code in cicatrix's link. Whatever you choose, good luck. I'm off to bed....wayyyy too sleepy atm.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Location
    Huddersfield, England
    Posts
    83

    Re: CRC16 generation

    Did you actually try the VB code in Visual Studio? if you try and calculate the checksum for "123456789" it should be 29B1 but it isn't!
    Last edited by consciouspnm; Apr 14th, 2012 at 02:24 PM.

  9. #9
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: CRC16 generation

    To get your result you should initialize the CRC16CCITT class with &HFFFF (NonZero1)
    vb Code:
    1. '
    2.     ' CRC16CCIT.ComputeCheckSum will return ushort with CRC16
    3.     ' CRC16CCIT.ComputeCheckSumBytes will return 2 bytes (hi and low)
    4.    
    5.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    6.         Dim checksumcalculator = New CRC16CCITT(CRC16CCITT.InitialCRCValue.NonZero1)
    7.         Dim value() As Byte = System.Text.Encoding.ASCII.GetBytes("123456789")
    8.         MsgBox(Format(checksumcalculator.ComputeCheckSum(value), "X2")) ' 29B1
    9.     End Sub
    10.  
    11.     Public Class CRC16CCITT
    12.         Public Enum InitialCRCValue
    13.             Zeroes = 0
    14.             NonZero1 = &HFFFF
    15.             NonZero2 = &H1D0F
    16.         End Enum
    17.  
    18.         Private Const poly As UShort = &H1021
    19.         Dim table(255) As UShort
    20.         Dim intValue As UShort = 0
    21.  
    22.         Public Function ComputeCheckSum(ByVal bytes As Byte()) As UShort
    23.             Dim crc As UShort = Me.intValue
    24.             For i As Integer = 0 To bytes.Length - 1
    25.                 crc = CUShort(((crc << 8) Xor table(((crc >> 8) Xor (&HFF And bytes(i))))))
    26.             Next
    27.             Return crc
    28.         End Function
    29.  
    30.         Public Function ComputeCheckSumBytes(ByVal bytes As Byte()) As Byte()
    31.             Dim crc As UShort = ComputeCheckSum(bytes)
    32.             Return BitConverter.GetBytes(crc)
    33.         End Function
    34.  
    35.         Public Sub New(ByVal initialvalue As InitialCRCValue)
    36.             Me.intValue = CUShort(initialvalue)
    37.             Dim temp, a As UShort
    38.             For i As Integer = 0 To table.Length - 1
    39.                 temp = 0
    40.                 a = CUShort(i << 8)
    41.                 For j As Integer = 0 To 7
    42.                     If ((temp Xor a) And &H8000) <> 0 Then
    43.                         temp = CUShort((temp << 1) Xor poly)
    44.                     Else
    45.                         temp <<= 1
    46.                     End If
    47.                     a <<= 1
    48.                 Next
    49.                 table(i) = temp
    50.             Next
    51.         End Sub
    52.     End Class

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Location
    Huddersfield, England
    Posts
    83

    Re: CRC16 generation

    Thanks, that worked fine, though copying and pasting the code out of your answer seemed to lose all the CR!

  11. #11
    New Member
    Join Date
    Jun 2012
    Posts
    3

    Re: CRC16 generation

    i have a calculator that calculate crc-16/-x25 "123456789" and i get '9B2E' then how do i do it in vb.net?
    Last edited by calvinpower; Jun 20th, 2012 at 08:31 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width