CRC test data

From BeebWiki
Jump to: navigation, search

This test data can be used to verify the working of the CRC-16 and CRC-32 code.

Test Data

String                        CRC-16 (xmodem)     CRC-32 (not XORed)
--------------------------------------------------------------------
"An Arbitrary String"         &DDFC               &90415518
"ZYXWVUTSRQPONMLKJIHGFEDBCA"  &B199               &6632024D
--------------------------------------------------------------------

To calculate the above and other CRC values the following small Perl script can be used:

#!/usr/bin/perl
# 2012-07-09 J.E. Klasek, j AT klasek at
# Validate known CRC values with Perl Digest::CRC routines.

use Digest::CRC qw(crc32 crc16 crcccitt crc crc8);

# s ... string for which the CRC is calculated
# crc16 ... hex value of the xmodem CRC
# crc32 ... hex value of the ZIP CRC, not XORed

@s=( { 's'=>"An Arbitrary String", 'crc16'=>"DDFC", 'crc32'=>"90415518" },
     { 's'=>"ZYXWVUTSRQPONMLKJIHGFEDBCA", 'crc16'=>"B199", 'crc32'=>"6632024D (not xored)" },
   );

print <<EOT ;
######################################################
# from Digest::CRC
#-----------------------------------------------------
# name,  [width,init,xorout,refout,poly,refin);
  crc8 => [8,0,0,0,0x07,0],
  crcccitt => [16,0xffff,0,0,0x1021,0],
  crc16 => [16,0,0,1,0x8005,1],
  crc32 => [32,0xffffffff,0xffffffff,1,0x04C11DB7,1],
######################################################
EOT

for my $tv (@s) {
    $s = $tv->{s};

    print  80x"#"."\n";
    print "# Testdata: \"$s\"\n\n";

    # crc($input,$width,$init,$xorout,$refout,$poly,$refin);

    printf "CRC32: %08X\n",crc32($s);
    printf "CRC32 (not xored): %08X (should be %s)\n",crc($s,32,0xffffffff,0x00000000,1,0x04C11DB7,1), $tv->{crc32};
    printf "CRC16: %04X\n",crc16($s);
    printf "CRCCCITT: %04X\n",crcccitt($s);
    printf "CRC16 xmodem: %04X (should be %s)\n",crc($s,16,0,0,0,0x1021,0),$tv->{crc16};
    printf "CRC8: %02X\n",crc8($s);
}