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

用Perl语言实现CRC-16算法和应用,第1张

概述#!/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

#!/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)

总结

以上是内存溢出为你收集整理的用Perl语言实现CRC-16算法和应用全部内容,希望文章能够帮你解决用Perl语言实现CRC-16算法和应用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1274297.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存