WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Files and Directories

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Checksum Calculations

 Keywords:  CRC BinaryChecksum pCheckSum Checksum Hex udfDecToHex

#DefineFunction udfDecToHex (intDecimal)
strHex = ""
intZ = 1
For intI = 7 To 0 By -1
   intN = (intDecimal >> (intI * 4)) & 15
   If !intN Then If intZ Then Continue
   intZ = 0
   strHex = StrCat (strHex, StrSub ("0123456789abcdef", IntN + 1, 1))
Next
Return strHex
#EndFunction


AddExtender ("wwser44i.dll")

; Allocate BinaryBuffer.
iBBSize = 1
hBB = BinaryAlloc (iBBSize)

; Prepare File Output.
IntControl (53, 0, 0, 0, 0)
sTempFile = FileCreateTemp ("TMP")
FileDelete (sTempFile)
sTempFolder = FilePath (sTempFile)
sFilename = "checksum.txt"
sFilename = StrCat (sTempFolder, sFilename)
hFW = FileOpen (sFilename, "WRITE")

; Write Header.
FileWrite (hFW, StrFixLeft ("BinaryChecksum_16x", "", 20))
FileWrite (hFW, StrFixLeft ("BinaryChecksum_32x", "", 20))
FileWrite (hFW, StrFixLeft ("pChecksum_16d", "", 20))
FileWrite (hFW, StrFixLeft ("pChecksum_16x", "", 20))
FileWrite (hFW, StrFixLeft ("pChecksum_32d", "", 20))
FileWrite (hFW, StrFixLeft ("pChecksum_32x", "", 20))
FileWrite (hFW, StrFixLeft ("pCheckBinary_16d", "", 20))
FileWrite (hFW, StrFixLeft ("pCheckBinary_32d", "", 20))
FileWrite (hFW, @CRLF)

; Do the test cases.
For i = 0 To 255
   sString = Num2Char (i)

   ; BinaryChecksum
   BinaryEodSet (hBB, 0)
   BinaryPokeStr (hBB, 0, sString)
   sCRC16Number1 = BinaryChecksum (hBB, 1)
   FileWrite (hFW, StrFixLeft (sCRC16Number1, "", 20))

   sCRC32Number1 = BinaryChecksum (hBB, 2)
   FileWrite (hFW, StrFixLeft (sCRC32Number1, "", 20))

   ; pChecksum
   sCRC16Number2 = pCheckSum (sString, 16)
   FileWrite (hFW, StrFixLeft (sCRC16Number2, "", 20))
   FileWrite (hFW, StrFixLeft (StrFixLeft (udfDecToHex (sCRC16Number2), "0", 4), "", 20))

   sCRC32Number2 = pCheckSum (sString, 32)
   FileWrite (hFW, StrFixLeft (sCRC32Number2, "", 20))
   FileWrite (hFW, StrFixLeft (StrFixLeft (udfDecToHex (sCRC32Number2), "0", 8), "", 20))

   ; pCheckBinary
   sCRC16Number3 = pCheckBinary (IntControl (42, hBB, 0, 0, 0), BinaryEodGet (hBB), 16)
   FileWrite (hFW, StrFixLeft (sCRC16Number3, "", 20))

   sCRC32Number3 = pCheckBinary (IntControl (42, hBB, 0, 0, 0), BinaryEodGet (hBB), 32)
   FileWrite (hFW, StrFixLeft (sCRC32Number3, "", 20))

   FileWrite (hFW, @CRLF)

Next


; Close file output
FileClose (hFW)

;Discard Buffer.
BinaryFree (hBB)

Run (sFilename, "")
Exit

;------------------------------------------------------------------------
;   Questions:
;   Why differ the results of the functions?
;   Different crc formulas used?
;
;   1.
;   case i=255: pChecksum_16d against pCheckBinary_16d
;
;   2.
;   case i=0: pChecksum_32x/pCheckBinary_32x against BinaryChecksum_32x
;
;   3.
;   BinaryChecksum_16x against pChecksum_16x
;
;   4.
;   BinaryChecksum_32x against pChecksum_32x
;
;   5.
;   BinaryChecksum_16x seems to return the underlying crc16 table for i=0 to 255;
;   Is this the correct behaviour?
;
;   6.
;   Is there a minimum length (in byte) needed to work correctly?
;
;------------------------------------------------------------------------

Article ID:   W18361
Filename:   Checksum Calculations .txt
File Created: 2009:06:26:12:17:00
Last Updated: 2009:06:26:12:17:00