I haven't been able to figure out how to enter a 32bit number, like your $input = 0xaabbccdd; into the website.(edit: holdover from my first draft; first line should not have been posted, since I figured it out before posting)

But if I want to match the results that the website gets for "AABBCCDD" as HEX input (https://crccalc.com/?crc=AABBCCDD&method=CRC-16/X-25&datatype=hex&outtype=0), which gives 0xF486, I use $cont=0 (I believe the cont parameter is "continue", and since you aren't adding to the string, but using it as the whole message, you want cont=>0 ) and $input = "\xAA\xBB\xCC\xDD" and it gives the same 0x4FCB that the online tool gives.

Fixing your cont->$cont typo to cont=>$cont , and adding a few different test cases, the test_it function has the right setup to get the same results as the online tool, as shown by the Test::More results.

#!perl.exe use 5.014; # //, strict, say, s///r use warnings; use Digest::CRC; my ($crc, $input, $width, $init, $xorout, $refout, $poly, $refin, $con +t, $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=>$xo +rout, 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&me +thod=CRC-16/X-25&datatype=ascii&outtype=0 test_it("\xAA", 0, 'FA28'); # https://crccalc.com/?crc=AA&method=C +RC-16/X-25&datatype=hex&outtype=0 test_it("\xAA\xBB\xCC\xDD", 0, '4FCB'); # https://crccalc.com/?crc=AA +BBCCDD&method=CRC-16/X-25&datatype=hex&outtype=0 test_it("\xDD\xCC\xBB\xAA", 0, 'F486'); # https://crccalc.com/?crc=DD +CCBBAA&method=CRC-16/X-25&datatype=hex&outtype=0 done_testing;

__END__ 1680 ok 1 - test_it("AABBCCDD", 0, 1D8D) ok 2 - test_it("\xAA", 0, FA28) ok 3 - test_it("\xAA\xBB\xCC\xDD", 0, 4FCB) ok 4 - test_it("\xDD\xCC\xBB\xAA", 0, F486) 1..4

I would have expected either test3 or test4 to match your 0xAABBCCDD input, but neither did.

And then I realized: 0xAABBCCDD is the integer 2864434397, which Perl stringifies into "2864434397", so if I try that string input, the tool gives 0x1680, which is what Digest::CRC gives for your $input = 0xaabbccdd; input.

Digest::CRC is built to work on a series of octets (because that's what it gets from reading a file), so when you pass in a number, it lets Perl stringify it into a string, which it treats as a series of octets; it doesn't deal with the raw octets for the integer (that would open the big-/little-/middle-endian can of worms). (Presumably, you could pack your integer as well, but that is left as an exercise for the reader.)


In reply to Re: CRC-16/X25 problem by pryrt
in thread CRC-16/X25 problem by bonzi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.