# Example for CRC16 ?

lukashaefliger
Junior MemberPosts:

**9**
Hello

Does anyone has a working example / function how to build CRC16-checksums?

Thanx :-)

Does anyone has a working example / function how to build CRC16-checksums?

Thanx :-)

0

## Comments

20Let?s look a t an example of a Cyclical Redundency Checking (CRC) CheckSum:

DEFINE_CALL 'CRC CALC' (LENGTH,STRING[30],H_CRC,L_CRC)

LOCAL_VAR COUNT1 COUNT2 CRC

{

CRC = 0

COUNT1 = 1

WHILE ( COUNT1 <= LENGTH )

{

COUNT2 = 1

CRC = CRC BXOR (STRING[COUNT1] * 256)

WHILE (COUNT2 < 9)

{

IF (CRC BAND $8000)

CRC = (CRC *2) BXOR $1021

ELSE

CRC = CRC * 2

COUNT2 = COUNT2 + 1

}

COUNT1 = COUNT1 + 1

}

H_CRC = CRC/256

L_CRC = CRC BAND 255

}

Here a 16 bit CheckSum is represented as the High and Low bytes. The first byte is multiplied by 256 and then BXORed with 0. The result is BANDed with $8000. If there is a 1 in the 16th bit the result is multiplied by 2 then BXORed with $1021, then the result is run through that process 7 more times. Then the second byte is BXORed with that result and that result is processed 8 times. This is repeated until all bits are done. Then we divide the 16 bits into two values, the High and Low CheckSum Bytes. The High by dividing and the Low by BANDing.

This example I found in paper "Bitwise Operations".

10Edited: Use code below

10Edited: Use code below

9I testet the code from kphlight because i have very much strings to transmit & calculate.

It looks like the fastest way....

But i get some Errors:

Ref Error ^CRC16_LOOKUP Index 0 Line=135

DoNumberExpression - Error 2 Tk=0x2012 Line=135

GetNumber - Error 1 Tk=0x0000

RunCode - Address Mismatch 0x4050 0x000704 0x000706

The problem ist, that the (t = (crc16retVal / 16) BXOR crcString

) returns "516" or "0"

Is the initializer with $FFFF correct???

Greetings & thanx fo help:

Lukas

10Here are cleaner chksum functions with lookup tables. It includes:

CRC16 (function crc16)

CRC16.CCITT (function crc16c)

CRC16.XMODEM (function crc16x)

CRC32 (function crc32)

I'm thinking about doing MD2/4/5, SHA1/256/384/512, Base64, CRC64.. anyone have any need of those?

294I'd love an MD5. :-)

20An implementation of Base64 can be found in i!-EquipmentMonitor, which is an "integration! Solutions" application and available for download from the AMX web site.

Take a look at i!-EquipmentMonitorOut.axi. The Base64 function included there is used for SMTP authentication (for sending emails from NetLinx).

Regards, Harald

450If you really need to use MD5 there is a module that I wrote that will perform MD5 encryption. Wont give the source code for the module away for various reasons, but I will send you the token. Just email me...

Below is how to use the module...

320Jeremy

4,3281Hello,

does anyone has an example for CRC16-checksum with a polynom 1002 ?

many thanks for help

45025I'm trying to implement the CRC16-CITT lookup as shown in the example, but getting a "Converting type [Long] to [INTEGER] when compiling. Sorry if this is a basic question, but really keen to use this checksum

Cheers

2,154nCRC = TYPE_CAST(nCRC << 1); //no warning

nCRC = nCRC << 1; //Long to Integer warning.

25its this line that seems to be causing the issue with Converting type [Long] to [INTEGER]

m_crc16c = g_pCRC16CcittTable[((m_crc16c >> 8) ^ crcString16c

)+1] ^ (m_crc16c << 8);450This is actually documented as a CRC16-CITT checksum as well...

25This seems to be compiling and producing a result I was looking for, although it seem to be inverted as I'm looking for $2C09 and getting $92C

450little endian vs big endian I would expect...

10Hello amx world,

Does anyone has a working example / function how to build CRC16-x25?

This is an example that I should calculate.

$00,$00,$03,$00,$12,$6c,$a5,$00,$00,$31,$31,$31,$31,$31,$31,$31,$31,$00 CRC low - CRC high ($D6,$2C)

Thanx.

6http://www.mcgougan.se/universal_crc/

http://www.repairfaq.org/filipg/LINK/F_crc_v31.html

1,485I realize that this a massive thread revival. How could this function be revised to calculate an 8 bit CRC?

Thank you!