yangcctv 发表于 2018-8-30 11:48:11

用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 == 49354))
  # {
  #   return;
  # }
  my $i = 0;
  my @lst;
  my $data;
  my $crc;
  while ($i < 256) {
  # body...
  $data = ($i>=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)

页: [1]
查看完整版本: 用Perl语言实现CRC-16算法和应用