码迷,mamicode.com
首页 > 编程语言 > 详细

用Perl语言实现CRC-16算法和应用

时间:2015-02-15 23:12:33      阅读:523      评论:0      收藏:0      [点我收藏+]

标签:perl语言; crc-16算法; 实现与应用

#!/usr/bin/eperl -w

#   Filename: crc-16.pl 

#

#   Copyright 2012 Axxeo GmbH

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

#   you may not use this file except in compliance with the License.

#   You may obtain a copy of the License at

#

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

#

#   Unless required by applicable law or agreed to in writing, software

#   distributed under the License is distributed on an "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) == 256) & ($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() #用于检查CRC码时候匹配

{

    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 # 此方法主要实现将字符串中的字符转换为十六进制,并加入:作为分隔符

{

    my $string = shift(@_);

    my @chrs = (unpack("(a)*", $string));

    my @newchrs = map { sprintf("%X", (ord($_)))} @chrs;

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

    return $iterms;

}



sub extraPayload # 上述方法的逆操作

{

    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)


本文出自 “EIT流浪汉” 博客,请务必保留此出处http://zicowarn.blog.51cto.com/3815716/1614595

用Perl语言实现CRC-16算法和应用

标签:perl语言; crc-16算法; 实现与应用

原文地址:http://zicowarn.blog.51cto.com/3815716/1614595

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!