Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: How to use perl digest module to calculate CRC?

by BrowserUk (Pope)
on Mar 21, 2013 at 11:25 UTC ( #1024721=note: print w/ replies, xml ) Need Help??


in reply to Re^2: How to use perl digest module to calculate CRC?
in thread How to use perl digest module to calculate CRC?

Can you recomend a proper crc calculator?

Do you mean code, or an on-line calculator?

Either way, the first thing to know is: why are you calculating a crc? What are you hoping it will do for you?


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.


Comment on Re^3: How to use perl digest module to calculate CRC?
Re^4: How to use perl digest module to calculate CRC?
by guyra (Novice) on Mar 21, 2013 at 12:05 UTC

    I have a file containing data in HEX.

    example:

    @0000 FF FF FF 02 24 E0 02 26 A9 02 21 B0 02 21 53 02

    @0010 21 6A 02 21 EE 02 21 F5 02 25 55 02 22 7B 02 23

    @0090 D2 AF D2 B8 75 1F 01 12 0B B1 75 15 00 E5 0B 20

    etc..

    I need to calculate the CRC for this data:

    my poly is non-standard.

    P=1edc6f41

    init value = 0xFFFFFFFF

    I'm going to use the digect::CRC to do it and I want some kind of a model as a reference to know that I do it correctly.

    By the way, as far as I understand, the digest::CRC consider the data as an ASCII string.

    Meaning it takes each char, gets it ascii value, and then convert it to BIN.

    This is not what I need.

    Ineed it to take the chars and consider them as a HEX value, and just convert them to BIN.

    Any idea on how I should achive it?

    Thx a lot

    Guy.

      I need to calculate the CRC

      You still aren't answering the question of why you need this?

      That is, the normal reason for calculating a crc is to detect transmission errors; but to do that, you need the crc as calculated prior to transmission to compare against. (Or if you are the transmitter; then a full description of the CRC algorithm expected by the receiver.

      I'll be more specific: is the CRC algorithm you wish to use 'Rocksoft Model CRC Algorithm'?

      If so, the full parameter set for teh algorithm should read something like:

      Name : "CRC-32C" Width : 32 Poly : 1EDC6F41 (note that the leading "1" is implied) Init : FFFFFFFF RefIn : True RefOut : True XorOut : FFFFFFFF Check : E3069283
      By the way, as far as I understand, the digest::CRC consider the data as an ASCII string. Meaning it takes each char, gets it ascii value, and then convert it to BIN. This is not what I need.

      Then you'll need to pre-pack the data before giving it to Digest::CRC.

      To do that, you'll need to know whether the CRC should be calculated for the data bytes alone; or should the preceding addresses be included some how?

      What format is the data file in? Ie. Where does it come from? What produces it?


      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.
Re^4: How to use perl digest module to calculate CRC?
by guyra (Novice) on Mar 21, 2013 at 14:18 UTC

    First, thx for your support!

    The reason I need it:

    I'm acting as a transmitter, and I need to calculate the CRC and then send it along with the message.

    The CRC algorithem that I'm using is not standard.

    P = 1EDC6F41

    Init value = FFFFFFFF

    The file that I'm working on is generated by an in-house script that we have.

    The address should not be part of the CRC calc. Only the HEX bytes.

    All the lines in the file should be considered as one message.

    So what should I do prior to send the data into the digest::CRC?

    Thx again :)

    Guy

      I'm acting as a transmitter, and I need to calculate the CRC and then send it along with the message.

      Okay. But you still need to ensure that you calculate it in the same way that the receiver does; which means you need the full parameterisation of the algorithm used.

      What you've supplied so far is not enough information:

      The CRC algorithem that I'm using is not standard. P = 1EDC6F41 Init value = FFFFFFFF

      Hm. Looks an awful lot like this.

      But without the full parameters, this is only guesswork:

      #! perl -slw use strict; use Digest::CRC; my $o = Digest::CRC->new( width => 32, init => 0xffffffff, poly => 0x1edc6f41, xorout => 0xffffffff, refout => 1, refin =>1, cont => 1 ); while( <DATA> ) { my( $addrr, @hexBytes ) = split; my $data = pack 'H*', join '', @hexBytes; $o->add( $data ); } print $o->hexdigest; __END__ @0000 FF FF FF 02 24 E0 02 26 A9 02 21 B0 02 21 53 02 @0010 21 6A 02 21 EE 02 21 F5 02 25 55 02 22 7B 02 23 @0090 D2 AF D2 B8 75 1F 01 12 0B B1 75 15 00 E5 0B 20

      Note: You'll probably need $o->digest rather than $o->hexdigest.

      NB: But the above will probably fail because it is just guesswork. Also, you'll need to retain the packed data for transmission.


      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.
Re^4: How to use perl digest module to calculate CRC?
by guyra (Novice) on Mar 21, 2013 at 16:15 UTC

    Still working on getting al the params.

    However, it seems that there is a problem with this approach.

    Not all HEX values have a "valid" ascii value that can be sent to the digest::crc.

    For example: if HEX=01 --> ASCII=SOH, which is not really a char that can be sent.

    That means that many HEX vals can't be in the game...

    Am I correct about that?

    If so, is there any other Perl olution for CRC on HEX data?

    Thx

      However, it seems that there is a problem with this approach. Not all HEX values have a "valid" ascii value that can be sent to the digest::crc. For example: if HEX=01 --> ASCII=SOH, which is not really a char that can be sent. That means that many HEX vals can't be in the game... Am I correct about that?

      No. SOH is just a byte with the numeric value 1, and it is a perfectly valid ASCII character. pack will pack it just fine.


      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.

        Thanks!

        indeed, the SOH does affect the CRC result.

        #data = (HEX) 3132 my $ctx = Digest::CRC->new(width => 32, poly => 0x04c11db7, init => +0xFFFFFFFF, xorout => 0xFFFFFFFF, refin => 1, refout => 1); my $r = $ctx->add( pack 'H*', '3132' )->hexdigest; print "data=", (pack 'H*', '3132'), "= | r=$r=\n"; #data = (ASCII) 12 my $ctx = Digest::CRC->new(width => 32, poly => 0x04c11db7, init => +0xFFFFFFFF, xorout => 0xFFFFFFFF, refin => 1, refout => 1); my $r = $ctx->add( "12" )->hexdigest; print "data=", (pack 'H*','3132'), "= | r=$r=\n"; #data = (HEX) 310132 my $ctx = Digest::CRC->new(width => 32, poly => 0x04c11db7, init => +0xFFFFFFFF, xorout => 0xFFFFFFFF, refin => 1, refout => 1); my $r = $ctx->add(pack 'H*','310132' )->hexdigest; print "data=", (pack 'H*', '310132'), "= | r=$r=\n";

        Results in:

        data=12= | r=4f5344cd= data=12= | r=4f5344cd= data=12= | r=b243674=

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2014-11-21 03:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (104 votes), past polls