#!perl use 5.014; # //, strict, say, s///r use warnings; use Digest::CRC; my ($crc, $input, $width, $init, $xorout, $refout, $poly, $refin, $cont, $digest, $ctx, $exp); $width = 16; $init = 0xffff; $xorout = 0xffff; $refout = 1; $poly = 0x1021; $refin = 1; $cont = 0; # ??? $input = 0xaabbccdd; $ctx = Digest::CRC->new(width=>$width, init=>$init, xorout=>$xorout, refout=>$refout, poly=>$poly, refin=>$refin, cont=>$cont); $ctx->add($input); $digest = $ctx->hexdigest; say $digest; use Test::More; use feature 'fc'; use Data::Dump qw/pp/; sub test_it { my($in, $cont, $expect) = @_; my $ctx = Digest::CRC->new(width=>$width, init=>$init, xorout=>$xorout, refout=>$refout, poly=>$poly, refin=>$refin, cont=>$cont); $ctx->add($in); my $got = $ctx->hexdigest; is fc($got), fc($expect), sprintf("test_it(%s, %s, %s)", pp($in), $cont, $expect); } test_it("AABBCCDD", 0, '1D8D'); # https://crccalc.com/?crc=AABBCCDD&method=CRC-16/X-25&datatype=ascii&outtype=0 test_it("\xAA", 0, 'FA28'); # https://crccalc.com/?crc=AA&method=CRC-16/X-25&datatype=hex&outtype=0 test_it("\xAA\xBB\xCC\xDD", 0, '4FCB'); # https://crccalc.com/?crc=AABBCCDD&method=CRC-16/X-25&datatype=hex&outtype=0 test_it("\xDD\xCC\xBB\xAA", 0, 'F486'); # https://crccalc.com/?crc=DDCCBBAA&method=CRC-16/X-25&datatype=hex&outtype=0 test_it(0xAABBCCDD, 0, '1680'); # 2864434397 test_it(2864434397, 0, '1680'); # 2864434397 test_it("2864434397", 0, '1680'); # https://crccalc.com/?crc=2864434397&method=CRC-16/X-25&datatype=ascii&outtype=0 done_testing;