За последние 24 часа нас посетили 16497 программистов и 1677 роботов. Сейчас ищут 938 программистов ...

Помогите плиз

Тема в разделе "PHP для новичков", создана пользователем dimpase, 28 май 2018.

  1. dimpase

    dimpase Активный пользователь

    С нами с:
    20 апр 2016
    Сообщения:
    12
    Симпатии:
    0
    есть функция

    Код (Text):
    1. function crc16_kermit($string) {
    2.  
    3.    $crc = 0;
    4.    for ( $x=0; $x<strlen( $string ); $x++ ) {
    5.  
    6.       $crc = $crc ^ ord( $string[$x] );
    7.       for ($y = 0; $y < 8; $y++) {
    8.  
    9.          if ( ($crc & 0x0001) == 0x0001 ) $crc = ( ($crc >> 1 ) ^ 0x1021 );
    10.          else                             $crc =    $crc >> 1;
    11.       }
    12.    }
    13.  
    14.    $lb  = ($crc & 0xff00) >> 8;
    15.    $hb  = ($crc & 0x00ff) << 8;
    16.    $crc = $hb | $lb;
    17.    //$crc = $lb | $hb;
    18.  
    19.    return $crc;
    20. }
    Входные данные hx ( 02030630 )
    Тут ( https://www.lammertbies.nl/comm/info/crc-calculation.html) считает правильно, контрольная сумма получается 0x41B3, но при использовании функции выше, я не получаю такого результата.
    Так же проверял контрольную сумму здесь (http://crccalc.com/), тоже правильно.
    Алгоритм : CRC-16/KERMIT
    У меня получается 46615. Или это число нужно еще во что то перевести?
     
  2. dimpase

    dimpase Активный пользователь

    С нами с:
    20 апр 2016
    Сообщения:
    12
    Симпатии:
    0
    Тему к закрытию. Здесь ни кто не помог!
    Выкладываю рабочий класс - не жадный:
    PHP:
    1. <?php
    2.  
    3.  
    4. /**
    5. * @author Philip Burggraf <philip@pburggraf.de>
    6. */
    7. abstract class AbstractCRC16 extends AbstractCRC
    8. {
    9.     /**
    10.      * @param string $buffer
    11.      *
    12.      * @return int
    13.      */
    14.     public function calculate(string $buffer): int
    15.     {
    16.         $crc = $this->init;
    17.         $bufferLength = strlen($buffer);
    18.         for ($bufferPosition = 0; $bufferPosition < $bufferLength; ++$bufferPosition) {
    19.             $character = ord($buffer[$bufferPosition]);
    20.             if ($this->reverseIn) {
    21.                 $character = $this->binaryReverse($character, 8);
    22.             }
    23.             $crc = $this->lookupTable[(($crc >> 8) ^ $character) & 0xff] ^ ($crc << 8);
    24.             $crc &= 0xffff;
    25.         }
    26.         if ($this->reverseOut) {
    27.             $crc = $this->binaryReverse($crc, 16);
    28.         }
    29.         return $crc ^ $this->xorOut;
    30.     }
    31.     /**
    32.      * @param int $polynomial
    33.      *
    34.      * @return array
    35.      */
    36.     public function generateTable(int $polynomial): array
    37.     {
    38.         $tableSize = 256;
    39.         $table = [];
    40.         for ($iterator = 0; $iterator < $tableSize; ++$iterator) {
    41.             $temp = 0;
    42.             $a = ($iterator << 8);
    43.             for ($j = 0; $j < 8; ++$j) {
    44.                 if ((($temp ^ $a) & 0x8000) !== 0) {
    45.                     $temp = (($temp << 1) ^ $polynomial);
    46.                 } else {
    47.                     $temp <<= 1;
    48.                 }
    49.                 $a <<= 1;
    50.             }
    51.             $table[$iterator] = $temp & 0xffff;
    52.         }
    53.         return $table;
    54.     }
    55. }
    56.  
    57.  
    58.  
    59. abstract class AbstractCRC
    60. {
    61.     /**
    62.      * @var int
    63.      */
    64.     protected $poly;
    65.     /**
    66.      * @var int
    67.      */
    68.     protected $init;
    69.     /**
    70.      * @var bool
    71.      */
    72.     protected $reverseIn;
    73.     /**
    74.      * @var bool
    75.      */
    76.     protected $reverseOut;
    77.     /**
    78.      * @var int
    79.      */
    80.     protected $xorOut;
    81.     /**
    82.      * @var array
    83.      */
    84.     protected $lookupTable;
    85.     /**
    86.      * @param string $buffer
    87.      *
    88.      * @return int
    89.      */
    90.     abstract public function calculate(string $buffer): int;
    91.     /**
    92.      * @param int $polynomial
    93.      *
    94.      * @return array
    95.      */
    96.     abstract protected function generateTable(int $polynomial): array;
    97.     /**
    98.      * @param int $binaryInput
    99.      * @param int $bitlen
    100.      *
    101.      * @return int
    102.      */
    103.     protected function binaryReverse(int $binaryInput, int $bitlen): int
    104.     {
    105.         $cloneBits = $binaryInput;
    106.         $binaryInput = 0;
    107.         $count = 0;
    108.         while ($count < $bitlen) {
    109.             $count++;
    110.             $binaryInput <<= 1;
    111.             $binaryInput |= ($cloneBits & 0x1);
    112.             $cloneBits >>= 1;
    113.         }
    114.         return $binaryInput;
    115.     }
    116. }
    117.  
    118.  
    119.  
    120.  
    121. class Kermit extends AbstractCRC16
    122. {
    123.     public function __construct()
    124.     {
    125.         $this->poly = 0x1021;
    126.         $this->init = 0x0000;
    127.         $this->reverseIn = true;
    128.         $this->reverseOut = true;
    129.         $this->xorOut = 0x0000;
    130.         $this->lookupTable = $this->generateTable($this->poly);
    131.     }
    132. }
    133.  
    134.  
    135.  
    136.  
    137.  
    138. $crc16Kermit = new Kermit();
    139. $str='02030630';
    140.  
    141. var_dump(dechex($crc16Kermit->calculate(hex2bin($str))))
    142.  
    143. ?>
     
  3. nospiou

    nospiou Старожил

    С нами с:
    4 фев 2018
    Сообщения:
    3.400
    Симпатии:
    510
    @dimpase А можешь по подробнее рассказать что там в методах происходит? а то копировать с интернета как то страшно вдруг там вирус.