CRC16

 BBS: Inland Empire Archive
Date: 02-14-93 (22:02)             Number: 304
From: DOUGLAS LUSHER               Refer#: NONE
  To: ALL                           Recvd: NO  
Subj: CRC16                          Conf: (2) Quik_Bas
 Someone requested code for 16 bit CRC not long ago. Some code
 was posted in response, but I thought I would post mine too. This
 routine should be much faster than the code posted previously, and
 if speed is critical, this can be made to run even faster with
 just a bit of ASM code. If some one requests it, I will post that.
 Also note that the previously posted code returns the CRC in a
 long integer, my routine returns an integer.

 DEFINT A-Z
 FUNCTION CRC16% (Target$)
 'returns the 16 bit CRC for the supplied string
 'by Douglas H. Lusher, 13 Feb 1993
 STATIC Initialized%, CRCTable%()

 IF Initialized% GOTO CalcCRC16

 REDIM CRCTable%(255)
 FOR Ptr% = 0 TO 255
   CRC& = Ptr% * 256&
   FOR Bit% = 0 TO 7
     Carry% = ((CRC& AND &H8000) <> 0)
     CRC& = (CRC& * 2) AND &HFFFF&
     CRC& = CRC& XOR (Carry% AND &H1021)
   NEXT
   CRCTable%(Ptr%) = (CRC& - 32768) XOR &H8000
 NEXT
 Initialized% = -1

 CalcCRC16:

 CRC& = 0
 FOR Ptr% = 1 TO LEN(Target$)
   Char% = ASC(MID$(Target$, Ptr%))
   CRC& = CRC& * &H100
   CRC& = CRCTable%(((CRC& AND &HFF0000)\ &H10000) XOR Char%) _
             XOR (CRC& AND &HFF00&)    'append this to the line above
 NEXT
 CRC16% = ((CRC& AND &HFFFF&) - 32768) XOR &H8000

 END FUNCTION


--- TMail v1.30.4
 * Origin: TC-AMS MLTBBS 2.2 - Minnetonka, MN (612)-938-4799 (1:282/7)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET