|
-
Apr 12th, 2012, 09:12 AM
#1
Thread Starter
Lively Member
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.
-
Apr 12th, 2012, 09:53 AM
#2
Re: CRC16 generation
Do you have a C or C++ code?
-
Apr 12th, 2012, 10:06 AM
#3
Re: CRC16 generation
Post the VB6 version. Ill help you convert it if I have time.
-
Apr 12th, 2012, 11:05 AM
#4
Thread Starter
Lively Member
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
-
Apr 12th, 2012, 12:04 PM
#5
Re: CRC16 generation
This is what they have there:-
vbnet Code:
' Private Function CRC16(ByVal crc As Integer, d As Byte) As Integer Dim carry As Integer, i As Byte For i = 0 To 7 carry = (crc And 1) Xor IIf(d And (2 ^ i), 1, 0) crc = (crc And &HFFFF&) \ 2 If carry <> 0 Then crc = crc Xor &H8408 Next i CRC16 = crc End Function Public Function CalcCrc16(ByVal buf As String) As String Dim crc As Integer, t As Integer crc = &H8408 For t = 1 To Len(buf) crc = CRC16(crc, Asc(Mid$(buf, t, 1))) Next t CalcCrc16 = Chr$(HiByte(crc)) & Chr$(LoByte(crc)) 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 ?
-
Apr 12th, 2012, 12:12 PM
#6
-
Apr 12th, 2012, 12:39 PM
#7
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:
' Private Declare Function RtlMoveMemory Lib "Kernel32" (ByRef dest As Byte, ByRef src As Short, ByVal cb As Integer) As Integer Private Function HiByte(ByVal num As Short) As Byte Dim arNum(1) As Byte RtlMoveMemory(arNum(0), num, 2) Return arNum(1) End Function Private Function LoByte(ByVal num As Short) As Byte Dim arNum(1) As Byte RtlMoveMemory(arNum(0), num, 2) Return arNum(0) 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.
-
Apr 14th, 2012, 01:31 PM
#8
Thread Starter
Lively Member
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.
-
Apr 14th, 2012, 07:32 PM
#9
Re: CRC16 generation
To get your result you should initialize the CRC16CCITT class with &HFFFF (NonZero1)
vb Code:
' ' CRC16CCIT.ComputeCheckSum will return ushort with CRC16 ' CRC16CCIT.ComputeCheckSumBytes will return 2 bytes (hi and low) Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Dim checksumcalculator = New CRC16CCITT(CRC16CCITT.InitialCRCValue.NonZero1) Dim value() As Byte = System.Text.Encoding.ASCII.GetBytes("123456789") MsgBox(Format(checksumcalculator.ComputeCheckSum(value), "X2")) ' 29B1 End Sub Public Class CRC16CCITT Public Enum InitialCRCValue Zeroes = 0 NonZero1 = &HFFFF NonZero2 = &H1D0F End Enum Private Const poly As UShort = &H1021 Dim table(255) As UShort Dim intValue As UShort = 0 Public Function ComputeCheckSum(ByVal bytes As Byte()) As UShort Dim crc As UShort = Me.intValue For i As Integer = 0 To bytes.Length - 1 crc = CUShort(((crc << 8) Xor table(((crc >> 8) Xor (&HFF And bytes(i)))))) Next Return crc End Function Public Function ComputeCheckSumBytes(ByVal bytes As Byte()) As Byte() Dim crc As UShort = ComputeCheckSum(bytes) Return BitConverter.GetBytes(crc) End Function Public Sub New(ByVal initialvalue As InitialCRCValue) Me.intValue = CUShort(initialvalue) Dim temp, 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 &H8000) <> 0 Then temp = CUShort((temp << 1) Xor poly) Else temp <<= 1 End If a <<= 1 Next table(i) = temp Next End Sub End Class
-
Apr 15th, 2012, 01:10 PM
#10
Thread Starter
Lively Member
Re: CRC16 generation
Thanks, that worked fine, though copying and pasting the code out of your answer seemed to lose all the CR!
-
Jun 20th, 2012, 08:11 PM
#11
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|