DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: fcs16 problem.. need help..

  1. #1
    Join Date
    Oct 2007
    Posts
    19

    fcs16 problem.. need help..

    lost 3 days but not able to solve it.
    please any one can help on this ??

    this module show error.
    at here
    CRCValue = (CRCShiftRight) Xor CRCArray(ArrayPointer)
    i don't know where i missed.
    no solution i have from 3 to 4 days i am trying to fix it but no idea at all.
    its fcs16 source but some how not working..
    any help on this topic?

    Option Explicit

    Public CRCArray As Variant

    Public Function CalcCRC(DataString As String) As String
    Dim DataLen As Integer
    Dim CRCValue As Long
    Dim CRCShiftRight As Integer
    Dim X As Integer
    Dim NextPieceofData As Integer
    Dim CRCShiftRightStr As String
    Dim ArrayPointer As Integer
    'invert
    Dim CRCIntStr As String
    Dim CRCFinalString As String
    Dim CRCletter As String
    Dim TempStr As String
    Dim CRCShiftRightLONG As Long

    'invert
    DataLen = Len(DataString)
    'init variables
    CRCShiftRight = 0
    ArrayPointer = 0
    ' Enought potatoes
    ' fcs = (fcs >> 8) ^ fcstab[(fcs ^ datapiece) & 0xff];
    ' int CRC to 0xffff
    CRCValue = &HFFFF
    ' Loop thru data
    For X = 1 To DataLen
    NextPieceofData = Asc(Mid(DataString, X, 1))
    ' SHIFT RIGHT (By turning to Hex movng letters and then back to integer)
    CRCShiftRightLONG = CRCValue And &HFF00
    'Long to short conversion
    If CRCShiftRightLONG < 32767 Then
    CRCShiftRight = CRCShiftRightLONG
    TempStr = Hex(CRCShiftRight)
    Else
    TempStr = Hex(CRCShiftRightLONG)
    End If
    ' End Long to short conversion
    'Take care of extra or not enough Hex Characters
    If Len(TempStr) = 4 Then
    CRCShiftRightStr = Mid(TempStr, 1, 2)
    Else
    If Len(TempStr) = 3 Then
    CRCShiftRightStr = Mid(TempStr, 1, 1)
    Else
    If Len(TempStr) < 3 Then
    CRCShiftRightStr = "00"
    Else
    If Len(TempStr) > 4 Then
    CRCShiftRightStr = "00"
    Debug.Print "lentempstr error"
    End If
    End If
    End If
    End If


    'End Take care of extra or not enough Hex Characters

    CRCShiftRight = Val("&H" & CRCShiftRightStr)

    ' END SHIFT RIGHT

    'ArrayPointer
    ArrayPointer = (CRCValue Xor NextPieceofData) And &HFF
    'End ArrayPointer


    'XOR
    CRCValue = (CRCShiftRight) Xor CRCArray(ArrayPointer)
    'End XOR
    Next

    'Fix neg Numbers
    If CRCValue < 0 Then
    CRCValue = 65536 + CRCValue
    End If
    'End Fix Ned Numbers



    CRCValue = CRCValue Xor &HFFFF
    CRCFinalString = Hex(CRCValue)


    Dim HiCRC As String
    Dim LoCRC As String
    Dim Hi As Byte
    Dim Lo As Byte

    HiCRC = Hex(CRCValue And &HFF)
    Hi = Val("&H" & HiCRC)
    LoCRC = Mid(CRCFinalString, 5, 2)
    Lo = Val("&H" & LoCRC)

    DataString = DataString & Chr(Hi) & Chr(Lo)
    CalcCRC = DataString

    End Function

    Public Sub Init()
    CRCArray = Array( _
    &H0, &H1189, &H2312, &H329B, &H4624, &H57AD, &H6536, &H74BF0, &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)
    End Sub

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    What is the error and what line of code is causing it?

  3. #3
    Join Date
    Oct 2007
    Posts
    19
    Quote Originally Posted by Hack View Post
    What is the error and what line of code is causing it?
    thx as always.

    at here this line..

    CRCValue = (CRCShiftRight) Xor CRCArray(ArrayPointer)

    run time 13
    type mismatch

  4. #4
    Join Date
    Mar 2008
    Location
    Lancashire, UK
    Posts
    156
    I can't get the error up either, what string are you passing to the function?

  5. #5
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    This (which was very difficult to follow)
    Code:
    If CRCShiftRightLONG < 32767 Then
    CRCShiftRight = CRCShiftRightLONG
    TempStr = Hex(CRCShiftRight)
    Else
    TempStr = Hex(CRCShiftRightLONG)
    End If
    ' End Long to short conversion
    'Take care of extra or not enough Hex Characters
    If Len(TempStr) = 4 Then
    CRCShiftRightStr = Mid(TempStr, 1, 2)
    Else
    If Len(TempStr) = 3 Then
    CRCShiftRightStr = Mid(TempStr, 1, 1)
    Else
    If Len(TempStr) < 3 Then
    CRCShiftRightStr = "00"
    Else
    If Len(TempStr) > 4 Then
    CRCShiftRightStr = "00"
    Debug.Print "lentempstr error"
    End If
    End If
    End If
    End If
    I translated into
    Code:
    If Len(TempStr) = 4 Then
        CRCShiftRightStr = Mid(TempStr, 1, 2)
    Else
        If Len(TempStr) = 3 Then
            CRCShiftRightStr = Mid(TempStr, 1, 1)
        Else
            If Len(TempStr) < 3 Then
                CRCShiftRightStr = "00"
            Else
                If Len(TempStr) > 4 Then
                    CRCShiftRightStr = "00"
                    Debug.Print "lentempstr error"
                End If
            End If
        End If
    End If
    which was easier to follow but has the potential to not do what you want it to if a specific conditions exists. So, I took that and translated it into
    Code:
    Select Case Len(TempStr)
         Case 4
            CRCShiftRightStr = Mid(TempStr, 1, 2)
         Case 3
            CRCShiftRightStr = Mid(TempStr, 1, 1)
         Case Is < 3
            CRCShiftRightStr = "00"
         Case Is > 4
            CRCShiftRightStr = "00"
            Debug.Print "lentempstr error"
    End Select
    Last edited by Hack; 11-05-2008 at 01:26 PM.

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    BTW: This is not how to declare a variable as an array
    Code:
    Public CRCArray As Variant
    You need the open/close parentheses in order for VB to recognize it as an array. Change that to:
    Code:
    Public CRCArray() As Variant
    Next question is: why a Variant?

  7. #7
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    Interesting!

    The way he is doing it, like:
    Public CRCArray As Variant
    creates a Variant holding an array of Integers

    Where as:
    Public CRCArray() As Variant
    creates a dynamic array of variants which would need A ReDim to Size it and each element would be a variant holding an Integer value

    Also by using a variant to hold the result of Array(...) he does not have to declare the size of the array ahead of time. If he adds more values to his list he does not need to change his variable declarations.

    I don't know enough about how VB stores this; but my guess would be that a variant holding an array of integers would be better than an array of variants where each element holds an integer.

    Of course the best way might be to create a dynamic array of Integers:
    Last edited by Ron Weller; 11-06-2008 at 09:54 AM.

Similar Threads

  1. Memory problem with Borland C3.1
    By AZ1699 in forum C++
    Replies: 11
    Last Post: 11-16-2007, 01:23 PM
  2. App Problem with MS Office ...
    By Shannon in forum VB Classic
    Replies: 7
    Last Post: 06-24-2007, 08:47 PM
  3. Replies: 0
    Last Post: 12-13-2001, 01:06 PM
  4. Arabic problem view
    By Ayman in forum VB Classic
    Replies: 0
    Last Post: 04-03-2000, 01:08 AM
  5. Problem with CryptoAPI and JCE
    By Jason Bock in forum VB Classic
    Replies: 0
    Last Post: 03-21-2000, 07:48 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center