Implementation of CRC-16 algorithm and application in Perl language

Source: Internet
Author: User
Tags ord sprintf unpack

#!/usr/bin/eperl-w

# filename:crc-16.pl

#

# Copyright Axxeo GmbH

# Licensed under the Apache License, Version 2.0 (the "License");

# You are not a use of this file except in compliance with the License.

# Obtain a copy of the License at

#

# http://www.apache.org/licenses/LICENSE-2.0

#

# unless required by applicable or agreed to writing, software

# Distributed under the License is distributed on a "as is" BASIS,

# without warranties or CONDITIONS of any KIND, either express or implied.

# See the License for the specific language governing permissions and

# Limitations under the License.

#



My @table;

Sub GenerateCRC16 ()

{

# globle @table;

# if (len (@table) = = & ($table [1] = = 49354))

# {

# return;

# }

my $i = 0;

My @lst;

My $data;

My $CRC;

while ($i < 256) {

# body ...

$data = ($i <<1);

$CRC = 0;

my $j = 8;

while ($j > 0)

{

$data >>=1;

if (($data ^ $crc) & 0x0001)

{

$CRC = ($CRC >> 1) ^ 0xa001;

}

Else

{

$CRC >>= 1;

}

$j-= 1;

}

$lst [$i] = $CRC;

$i +=1;

}

return @lst;

}


@table = generateCRC16 ();


print "-----------------------------------------------\ n";

Print "The following is the crc-16 table:\n";


my $c = 1;

For $a (@table)

{

printf ("0x%X", $a);

print "\ t";

if (($c% 8 = = 0) & ($c! = 0))

{

print "\ n";

}

$c + = 1;

}


print "-----------------------------------------------\ n";

Sub Calculatercrc ()

{

My $string = Shift (@_);

my $CRC = 0xFFFF;

#foreach $CHR (Unpack ("(a) *", $string))

foreach $chr (Unpack ("c*", $string))

{

$CRC = ($CRC >> 8) ^ $table [($crc ^ $chr) & 0xFF];

}

My $crcL = sprintf ("\\x%X", &_lo ($CRC));

My $crcH = sprintf ("\\x%X", &_hi ($CRC));

return $crcH. $crcL;

}


#printf ("%x\n", &CALCULATERCRC ("Hallo World");


Sub Convertchrtoacsii ()

{

My $string = Shift (@_);

foreach $chr (Unpack ("C0u4", $string))

{

Print $CHR. "The Acsii code is:". Ord ($CHR). In hex format: ";

printf "%x\n", (Ord ($CHR));

}

Return

}


Sub _lo ()

{

My $myhex = Shift (@_);

Return ($myhex & 0x00FF);

}



Sub _hi ()

{

My $myhex = Shift (@_);

Return (($myhex & 0xff00) >> 8);

}


Sub Checkcrc () # is used to check the CRC code when matching

{

My ($payload, $crcsum) = @_;

Print $payload. " ---\ n ";

Print $crcsum. " +++\n ";

if ($crcsum eq &CALCULATERCRC ($payload))

{

print "Check CRC summe>>: Not match!! \ n ";

return 1;

}

Else

{

print "Check CRC summe>>: match!! \ n ";

return 0;

}

}


Sub Embedpayload # This method primarily implements converting characters in a string to 16, and adding: as a delimiter

{

My $string = Shift (@_);

My @chrs = (Unpack ("(a) *", $string));

My @newchrs = map {sprintf ("%x", (Ord ($_))} @chrs;

My $iterms =join (":", @newchrs);

return $iterms;

}



Sub Extrapayload # Inverse operation of the above method

{

My $iterms = Shift (@_);

My @chrs = Split (":", $iterms);

My @newchrs = map {chr (hex ($_))} @chrs;

My $string =join ("", @newchrs);

return $string;

}


print "-----------------------------------------------\ n";


Print &embedpayload ("ABCD"). " \ n ";

print "---------\ n";

Print &extrapayload (&embedpayload ("ABCD"));

#print chr ("\x4f\x4b");

# Usage:

# for example

# Payload of message is ' Hallo world '

# PAYLOAD+CRC (Hi) +CRC (Lo)


This article is from the "EIT Tramp" blog, please be sure to keep this source http://zicowarn.blog.51cto.com/3815716/1614595

Implementation of CRC-16 algorithm and application in Perl language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.