Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

CRC 16

by igor1212 (Novice)
on Oct 08, 2012 at 07:46 UTC ( #997760=perlquestion: print w/ replies, xml ) Need Help??
igor1212 has asked for the wisdom of the Perl Monks concerning the following question:

Hello to all. I need to do a crc 16 calculation in perl. My example data is 6401(hex)and CRC should be: 13BC (hex).

I've tried every combination using digest::crc and string::crc, but I've got no correct crc value.

I've received a c function for crc calculation from my device vendor, but I need this in Perl.

C Function:

unsigned short crc_16_rec (char *pucData, unsigned short ucLen) { unsigned int i; unsigned char ucBit, ucCarry; unsigned short usPoly = 0x8408;//reversed 0x1021 unsigned short usCRC = 0; for (i = 0; i < ucLen; i++) { usCRC ^= pucData[i]; for (ucBit = 0; ucBit < 8; ucBit++) { ucCarry = usCRC & 1; usCRC >>= 1; if (ucCarry) { usCRC ^= usPoly; } } } return usCRC; }

Can somebody please help me with this, because I'm stuck here!

Best Regards!

Igor

Comment on CRC 16
Download Code
Re: CRC 16
by BrowserUk (Pope) on Oct 08, 2012 at 08:21 UTC

    I can't see how 0x8404 is the 'reverse' of 0x1021 by any stretch of my imagination,(*)

    but a simple brute force conversion yields:

    #! perl -slw use strict; sub crc16 { use constant POLY => 0x8408; my $crc = 0; for my $c ( unpack 'C*', $_[0] ) { $crc ^= $c; for my $b ( 0 .. 7 ) { my $carry = $crc & 1; $crc >>= 1; if( $carry ) { $crc ^= POLY; } } } return $crc; } my $data = "\x64\x01"; printf "crc: %04x\n", crc16( $data ); __END__ C:\test>997760.pl crc: 13bc

    Which works for the (inadequate, single) sample data.

    (* 0x1021 -> 0001_0000_0010_0001 bitwise reversed -> 1000_0100_0000_1000 -> 0x8408 )


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: CRC 16
by moritz (Cardinal) on Oct 08, 2012 at 12:36 UTC

      It's not the same algorithm.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      RIP Neil Armstrong

        Hello BrowserUk,

        Your solution did the work perfectly!

        Thanks!

        Igor

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997760]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2014-10-23 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (129 votes), past polls