Public Class Form1
Private Function getCrc16(b() As Byte, nLen As Integer) As UInt16

             {&H0, &H1189, &H2312, &H329B, &H4624, &H57AD, &H6536, &H74BF, _
              &H8C48, &H9DC1, &HAF5A, &HBED3, &HCA6C, &HDBE5, &HE97E, &HF8F7, _
              &H1081, &H108, &H3393, &H221A, &H56A5, &H472C, &H75B7, &H643E, _
              &H9CC9, &H8D40, &HBFDB, &HAE52, &HDAED, &HCB64, &HF9FF, &HE876, _
              &H2102, &H308B, &H210, &H1399, &H6726, &H76AF, &H4434, &H55BD, _
              &HAD4A, &HBCC3, &H8E58, &H9FD1, &HEB6E, &HFAE7, &HC87C, &HD9F5, _
              &H3183, &H200A, &H1291, &H318, &H77A7, &H662E, &H54B5, &H453C, _
              &HBDCB, &HAC42, &H9ED9, &H8F50, &HFBEF, &HEA66, &HD8FD, &HC974, _
              &H4204, &H538D, &H6116, &H709F, &H420, &H15A9, &H2732, &H36BB, _
              &HCE4C, &HDFC5, &HED5E, &HFCD7, &H8868, &H99E1, &HAB7A, &HBAF3, _
              &H5285, &H430C, &H7197, &H601E, &H14A1, &H528, &H37B3, &H263A, _
              &HDECD, &HCF44, &HFDDF, &HEC56, &H98E9, &H8960, &HBBFB, &HAA72, _
              &H6306, &H728F, &H4014, &H519D, &H2522, &H34AB, &H630, &H17B9, _
              &HEF4E, &HFEC7, &HCC5C, &HDDD5, &HA96A, &HB8E3, &H8A78, &H9BF1, _
              &H7387, &H620E, &H5095, &H411C, &H35A3, &H242A, &H16B1, &H738, _
              &HFFCF, &HEE46, &HDCDD, &HCD54, &HB9EB, &HA862, &H9AF9, &H8B70, _
              &H8408, &H9581, &HA71A, &HB693, &HC22C, &HD3A5, &HE13E, &HF0B7, _
              &H840, &H19C9, &H2B52, &H3ADB, &H4E64, &H5FED, &H6D76, &H7CFF, _
              &H9489, &H8500, &HB79B, &HA612, &HD2AD, &HC324, &HF1BF, &HE036, _
              &H18C1, &H948, &H3BD3, &H2A5A, &H5EE5, &H4F6C, &H7DF7, &H6C7E, _
              &HA50A, &HB483, &H8618, &H9791, &HE32E, &HF2A7, &HC03C, &HD1B5, _
              &H2942, &H38CB, &HA50, &H1BD9, &H6F66, &H7EEF, &H4C74, &H5DFD, _
              &HB58B, &HA402, &H9699, &H8710, &HF3AF, &HE226, &HD0BD, &HC134, _
              &H39C3, &H284A, &H1AD1, &HB58, &H7FE7, &H6E6E, &H5CF5, &H4D7C, _
              &HC60C, &HD785, &HE51E, &HF497, &H8028, &H91A1, &HA33A, &HB2B3, _
              &H4A44, &H5BCD, &H6956, &H78DF, &HC60, &H1DE9, &H2F72, &H3EFB, _
              &HD68D, &HC704, &HF59F, &HE416, &H90A9, &H8120, &HB3BB, &HA232, _
              &H5AC5, &H4B4C, &H79D7, &H685E, &H1CE1, &HD68, &H3FF3, &H2E7A, _
              &HE70E, &HF687, &HC41C, &HD595, &HA12A, &HB0A3, &H8238, &H93B1, _
              &H6B46, &H7ACF, &H4854, &H59DD, &H2D62, &H3CEB, &HE70, &H1FF9, _
              &HF78F, &HE606, &HD49D, &HC514, &HB1AB, &HA022, &H92B9, &H8330, _
              &H7BC7, &H6A4E, &H58D5, &H495C, &H3DE3, &H2C6A, &H1EF1, &HF78}

        Dim fcs As UInt16 = &HFFFF
        For i = 0 To nLen - 1
            fcs = (fcs >> 8) Xor crctab16((fcs Xor b(i)) And &HFF)
        Next
        Return Not fcs

    End Function

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim s As String = "0D0103588990532174470001"
        Dim inp(s.Length \ 2 - 1) As Byte 'allocate byte array storage assumes string length is even, 2 chars per hex byte
        For i = 0 To inp.Length - 1  'for each byte in the array
            inp(i) = Convert.ToByte(s.Substring(i * 2, 2), 16) 'convert 2 hex chars from the string into a byte value
        Next
        TextBox1.Text = (getCrc16(inp, inp.Length).ToString("X"))
 End Sub
End Class  

This is VS2010(vb.net) code i want to convert it in visual basic 6.0.
is there any one who can solve this issue???

Dani AI

Generated

Short context: posted a VB.NET CRC-16 routine (table-based) and asked for a VB6 conversion; correctly pointed out the original code uses .NET-only types/syntax. VB6 has no UInt16 and no >>/<< operators, so you should use Long for arithmetic, mask to &HFFFF to keep 16-bit results, and use arithmetic (multiply/divide) for shifts.

The VB6 drop-in below computes the same CRC-16 (table-less, MSB-first) and includes a helper to convert a hex string to a Byte array. It avoids copying the large lookup table from your post and will produce the same 16-bit value (masking and byte-order issues discussed after the code).

Public Function GetCrc16(data() As Byte, nLen As Long) As Long
    Dim crc As Long, i As Long, j As Integer
    crc = &HFFFF&
    For i = 0 To nLen - 1
        crc = crc Xor (CLng(data(i)) * 256)    'byte << 8
        For j = 1 To 8
            If (crc And &H8000&) <> 0 Then
                crc = ((crc * 2) Xor &H1021&) And &HFFFF&
            Else
                crc = (crc * 2) And &HFFFF&
            End If
        Next j
    Next i
    GetCrc16 = (Not crc) And &HFFFF&    'final XOR (16-bit)
End Function

Public Function HexToBytes(hexStr As String) As Variant
    Dim i As Long, n As Long
    Dim b() As Byte
    hexStr = Replace$(hexStr, " ", "")
    n = Len(hexStr) \ 2
    If n = 0 Then HexToBytes = Array(): Exit Function
    ReDim b(0 To n - 1) As Byte
    For i = 0 To n - 1
        b(i) = CByte(Val("&H" & Mid$(hexStr, i * 2 + 1, 2)))
    Next i
    HexToBytes = b
End Function

Quick tips/troubleshooting

  • Use Option Explicit and declare all variables as Long/Integer where shown. Mask with &HFFFF& after arithmetic to avoid sign/overflow issues.
  • If your VB6 result doesn't match the VB.NET result, check byte order: some protocols expect low-byte first (swap hi/lo).
  • To display hex: use Right$("0000" & Hex$(GetCrc16(arr, UBound(arr)+1)), 4).
  • If further mismatch persists, post the exact hex input and the expected CRC from the .NET run so we can verify polynomial/reflect/init/final settings.

Recommended Answers

All 2 Replies

This is close to impossible since that code won't compile as is and it references objects that are created elsewhere.

thnx to reply tinstaafl... its is basically crc 16 error sum check value program. sorry there was mistake in copy paste again i send you the code again..please go through it if u can....its important to me.. if any1 can help

` Private Function getCrc16(b() As Byte, nLen As Integer) As UInt16

Static crctab16() As UInt16 = _
      {&H0, &H1189, &H2312, &H329B, &H4624, &H57AD, &H6536, &H74BF, _
      &H8C48, &H9DC1, &HAF5A, &HBED3, &HCA6C, &HDBE5, &HE97E, &HF8F7, _
      &H1081, &H108, &H3393, &H221A, &H56A5, &H472C, &H75B7, &H643E, _
      &H9CC9, &H8D40, &HBFDB, &HAE52, &HDAED, &HCB64, &HF9FF, &HE876, _
      &H2102, &H308B, &H210, &H1399, &H6726, &H76AF, &H4434, &H55BD, _
      &HAD4A, &HBCC3, &H8E58, &H9FD1, &HEB6E, &HFAE7, &HC87C, &HD9F5, _
      &H3183, &H200A, &H1291, &H318, &H77A7, &H662E, &H54B5, &H453C, _
      &HBDCB, &HAC42, &H9ED9, &H8F50, &HFBEF, &HEA66, &HD8FD, &HC974, _
      &H4204, &H538D, &H6116, &H709F, &H420, &H15A9, &H2732, &H36BB, _
      &HCE4C, &HDFC5, &HED5E, &HFCD7, &H8868, &H99E1, &HAB7A, &HBAF3, _
      &H5285, &H430C, &H7197, &H601E, &H14A1, &H528, &H37B3, &H263A, _
      &HDECD, &HCF44, &HFDDF, &HEC56, &H98E9, &H8960, &HBBFB, &HAA72, _
      &H6306, &H728F, &H4014, &H519D, &H2522, &H34AB, &H630, &H17B9, _
      &HEF4E, &HFEC7, &HCC5C, &HDDD5, &HA96A, &HB8E3, &H8A78, &H9BF1, _
      &H7387, &H620E, &H5095, &H411C, &H35A3, &H242A, &H16B1, &H738, _
      &HFFCF, &HEE46, &HDCDD, &HCD54, &HB9EB, &HA862, &H9AF9, &H8B70, _
      &H8408, &H9581, &HA71A, &HB693, &HC22C, &HD3A5, &HE13E, &HF0B7, _
      &H840, &H19C9, &H2B52, &H3ADB, &H4E64, &H5FED, &H6D76, &H7CFF, _
      &H9489, &H8500, &HB79B, &HA612, &HD2AD, &HC324, &HF1BF, &HE036, _
      &H18C1, &H948, &H3BD3, &H2A5A, &H5EE5, &H4F6C, &H7DF7, &H6C7E, _
      &HA50A, &HB483, &H8618, &H9791, &HE32E, &HF2A7, &HC03C, &HD1B5, _
      &H2942, &H38CB, &HA50, &H1BD9, &H6F66, &H7EEF, &H4C74, &H5DFD, _
      &HB58B, &HA402, &H9699, &H8710, &HF3AF, &HE226, &HD0BD, &HC134, _
      &H39C3, &H284A, &H1AD1, &HB58, &H7FE7, &H6E6E, &H5CF5, &H4D7C, _
      &HC60C, &HD785, &HE51E, &HF497, &H8028, &H91A1, &HA33A, &HB2B3, _
      &H4A44, &H5BCD, &H6956, &H78DF, &HC60, &H1DE9, &H2F72, &H3EFB, _
      &HD68D, &HC704, &HF59F, &HE416, &H90A9, &H8120, &HB3BB, &HA232, _
      &H5AC5, &H4B4C, &H79D7, &H685E, &H1CE1, &HD68, &H3FF3, &H2E7A, _
      &HE70E, &HF687, &HC41C, &HD595, &HA12A, &HB0A3, &H8238, &H93B1, _
      &H6B46, &H7ACF, &H4854, &H59DD, &H2D62, &H3CEB, &HE70, &H1FF9, _
      &HF78F, &HE606, &HD49D, &HC514, &HB1AB, &HA022, &H92B9, &H8330, _
      &H7BC7, &H6A4E, &H58D5, &H495C, &H3DE3, &H2C6A, &H1EF1, &HF78}

        Dim fcs As UInt16 = &HFFFF
        For i = 0 To nLen - 1
            fcs = (fcs >> 8) Xor crctab16((fcs Xor b(i)) And &HFF)
        Next
        Return Not fcs

End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
for the first case, D4CF for the second case

   Private Function getCrc16(b() As Byte, nLen As Integer) As UInt16

    Static crctab16() As UInt16 = _
          {&H0, &H1189, &H2312, &H329B, &H4624, &H57AD, &H6536, &H74BF, _
          &H8C48, &H9DC1, &HAF5A, &HBED3, &HCA6C, &HDBE5, &HE97E, &HF8F7, _
          &H1081, &H108, &H3393, &H221A, &H56A5, &H472C, &H75B7, &H643E, _
          &H9CC9, &H8D40, &HBFDB, &HAE52, &HDAED, &HCB64, &HF9FF, &HE876, _
          &H2102, &H308B, &H210, &H1399, &H6726, &H76AF, &H4434, &H55BD, _
          &HAD4A, &HBCC3, &H8E58, &H9FD1, &HEB6E, &HFAE7, &HC87C, &HD9F5, _
          &H3183, &H200A, &H1291, &H318, &H77A7, &H662E, &H54B5, &H453C, _
          &HBDCB, &HAC42, &H9ED9, &H8F50, &HFBEF, &HEA66, &HD8FD, &HC974, _
          &H4204, &H538D, &H6116, &H709F, &H420, &H15A9, &H2732, &H36BB, _
          &HCE4C, &HDFC5, &HED5E, &HFCD7, &H8868, &H99E1, &HAB7A, &HBAF3, _
          &H5285, &H430C, &H7197, &H601E, &H14A1, &H528, &H37B3, &H263A, _
          &HDECD, &HCF44, &HFDDF, &HEC56, &H98E9, &H8960, &HBBFB, &HAA72, _
          &H6306, &H728F, &H4014, &H519D, &H2522, &H34AB, &H630, &H17B9, _
          &HEF4E, &HFEC7, &HCC5C, &HDDD5, &HA96A, &HB8E3, &H8A78, &H9BF1, _
          &H7387, &H620E, &H5095, &H411C, &H35A3, &H242A, &H16B1, &H738, _
          &HFFCF, &HEE46, &HDCDD, &HCD54, &HB9EB, &HA862, &H9AF9, &H8B70, _
          &H8408, &H9581, &HA71A, &HB693, &HC22C, &HD3A5, &HE13E, &HF0B7, _
          &H840, &H19C9, &H2B52, &H3ADB, &H4E64, &H5FED, &H6D76, &H7CFF, _
          &H9489, &H8500, &HB79B, &HA612, &HD2AD, &HC324, &HF1BF, &HE036, _
          &H18C1, &H948, &H3BD3, &H2A5A, &H5EE5, &H4F6C, &H7DF7, &H6C7E, _
          &HA50A, &HB483, &H8618, &H9791, &HE32E, &HF2A7, &HC03C, &HD1B5, _
          &H2942, &H38CB, &HA50, &H1BD9, &H6F66, &H7EEF, &H4C74, &H5DFD, _
          &HB58B, &HA402, &H9699, &H8710, &HF3AF, &HE226, &HD0BD, &HC134, _
          &H39C3, &H284A, &H1AD1, &HB58, &H7FE7, &H6E6E, &H5CF5, &H4D7C, _
          &HC60C, &HD785, &HE51E, &HF497, &H8028, &H91A1, &HA33A, &HB2B3, _
          &H4A44, &H5BCD, &H6956, &H78DF, &HC60, &H1DE9, &H2F72, &H3EFB, _
          &HD68D, &HC704, &HF59F, &HE416, &H90A9, &H8120, &HB3BB, &HA232, _
          &H5AC5, &H4B4C, &H79D7, &H685E, &H1CE1, &HD68, &H3FF3, &H2E7A, _
          &HE70E, &HF687, &HC41C, &HD595, &HA12A, &HB0A3, &H8238, &H93B1, _
          &H6B46, &H7ACF, &H4854, &H59DD, &H2D62, &H3CEB, &HE70, &H1FF9, _
          &HF78F, &HE606, &HD49D, &HC514, &HB1AB, &HA022, &H92B9, &H8330, _
          &H7BC7, &H6A4E, &H58D5, &H495C, &H3DE3, &H2C6A, &H1EF1, &HF78}

            Dim fcs As UInt16 = &HFFFF
            For i = 0 To nLen - 1
                fcs = (fcs >> 8) Xor crctab16((fcs Xor b(i)) And &HFF)
            Next
            Return Not fcs

End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
for the first case, D4CF for the second case 
        Dim s As String = "0D010358899053217447000A"
        Dim inp(s.Length \ 2 - 1) As Byte 
            For i = 0 To inp.Length - 1  
                inp(i) = Convert.ToByte(s.Substring(i * 2, 2), 16) 
            Next
            TextBox1.Text = (getCrc16(inp, inp.Length).ToString("X"))
        End Sub

End Class


    Dim inp(s.Length \ 2 - 1) As Byte 
        For i = 0 To inp.Length - 1  
            inp(i) = Convert.ToByte(s.Substring(i * 2, 2), 16) 
        Next
        TextBox1.Text = (getCrc16(inp, inp.Length).ToString("X"))
    End Sub

End Class`

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.