I have a file containing approx 300 lines of text..
Each line has a CrLf at the end. The size of the file is about 8K.
I am reading strings into an array.
I need a simple method of creating a checksum for the file.
Can anyone suggest a fast and simple method.
Thanks
Charles W.
> I have a file containing approx 300 lines of text..
> Each line has a CrLf at the end. The size of the file is about 8K.
[quoted text clipped - 7 lines]
>
> Charles W.
'modCRC32.bas
Option Explicit
Private malTable(255) As Long
Public Sub Main()
Dim sFile As String
sFile = App.Path
If Right$(sFile, 1) <> "\" Then sFile = sFile & "\"
sFile = sFile & "modCRC32.bas"
Debug.Print GetFileCRC(sFile)
End Sub
Private Function AddCrc32(ByVal sItem As String, ByVal lCRC32 As Long)
As Long
Dim yCharValue As Byte
Dim lCounter As Long
Dim lIndex As Long
Dim lAccValue As Long
Dim lTableValue As Long
For lCounter = 1 To Len(sItem)
yCharValue = Asc(Mid$(sItem, lCounter, 1))
lAccValue = lCRC32 And &HFFFFFF00
lAccValue = lAccValue \ &H100
lAccValue = lAccValue And &HFFFFFF
lIndex = lCRC32 And &HFF
lIndex = lIndex Xor yCharValue
lTableValue = malTable(lIndex)
lCRC32 = lAccValue Xor lTableValue
Next lCounter
AddCrc32 = lCRC32
End Function
Public Function ComputeCRC32(ByRef sString As String) As String
Dim lCrc32Value As Long
lCrc32Value = InitCrc32()
lCrc32Value = AddCrc32(sString, lCrc32Value)
ComputeCRC32 = Hex$(GetCrc32(lCrc32Value))
End Function
Public Function FileExists(ByVal sFilePath As String) As Boolean
Dim nAttr As Integer
FileExists = False
If Len(sFilePath) = 0 Or InStr(sFilePath, "*") > 0 Or
InStr(sFilePath, "?") > 0 Then
Exit Function
End If
On Error GoTo ErrorFileExist
nAttr = VBA.GetAttr(sFilePath)
FileExists = ((nAttr And vbDirectory) = 0)
ErrorFileExist:
End Function
Private Function GetCrc32(ByVal lCRC32 As Long) As Long
GetCrc32 = lCRC32 Xor &HFFFFFFFF
End Function
Public Function GetFileCRC(ByRef sFile As String) As String
Dim nF As Integer
Dim lFileLen As Long
Dim sCRC32 As String
Dim sFileString As String
sCRC32 = ""
If FileExists(sFile) Then
lFileLen = FileLen(sFile)
sFileString = String$(lFileLen, 0)
nF = FreeFile
Open sFile For Binary As #nF
Get #nF, 1, sFileString
Close #nF
sCRC32 = ComputeCRC32(sFileString)
End If
GetFileCRC = sCRC32
End Function
Private Function InitCrc32(Optional ByVal Seed As Long = &HEDB88320,
Optional ByVal Precondition As Long = &HFFFFFFFF) As Long
Dim iBytes As Integer
Dim iBits As Integer
Dim lCRC32 As Long
Dim lTempCrc32 As Long
For iBytes = 0 To 255
lCRC32 = iBytes
For iBits = 0 To 7
lTempCrc32 = lCRC32 And &HFFFFFFFE
lTempCrc32 = lTempCrc32 \ &H2
lTempCrc32 = lTempCrc32 And &H7FFFFFFF
If (lCRC32 And &H1) <> 0 Then
lCRC32 = lTempCrc32 Xor Seed
Else
lCRC32 = lTempCrc32
End If
Next iBits
malTable(iBytes) = lCRC32
Next iBytes
InitCrc32 = Precondition
End Function
charles@home.com - 21 Jul 2008 18:48 GMT
Jason
When I run this I dont get any result.
Can you help?
Thanks
Charles W
>> I have a file containing approx 300 lines of text..
>> Each line has a CrLf at the end. The size of the file is about 8K.
[quoted text clipped - 144 lines]
>
> End Function
Alfie [UK] - 21 Jul 2008 19:20 GMT
>When I run this I dont get any result.
>
>Can you help?
The module as it stands outputs the result to the immediate window which
will hide itself after running if you don't have it showing normally.
Either [View], the [Immediate Window], or change the line;
Debug.Print GetFileCRC(sFile)
...to...
MsgBox GetFileCRC(sFile)
Obviously you'll need to tailor usage and output to your own app.
HTH

Signature
Alfie [UK]
<http://www.delphia.co.uk/>
Forgive me if I can't give you the answers today, I don't have all the answers, and the answers I do have may be different tomorrow.
charles@home.com - 21 Jul 2008 23:01 GMT
Thanks. I did manage to get this to work.
Charles W.
>>When I run this I dont get any result.
>>
[quoted text clipped - 14 lines]
>
> HTH
Jason Keats - 22 Jul 2008 12:17 GMT
> Thanks. I did manage to get this to work.
>
> Charles W.
Good :-)