CRC-CCITT(0xFFFF)功能? [英] CRC-CCITT (0xFFFF) function?

查看:394
本文介绍了CRC-CCITT(0xFFFF)功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我使用Delphi实现CRC-CCITT( 0xFFFF )?



已经获得了Java版本,但混淆如何将其移植到Delphi

  public static int CRC16CCITT(byte []字节){
int crc = 0xFFFF; //初始值
int polynomial = 0x1021; // 0001 0000 0010 0001(0,5,12)

for(byte b:bytes){
for(int i = 0; i< 8; i ++){
boolean bit =((b>>(7-i)& 1)== 1);
boolean c15 =((crc>>> 15& 1)== 1);
crc<< = 1;
if(c15 ^ bit)crc ^ = polynomial;
}
}

crc& = 0xffff;
//System.out.println(\"CRC16-CCITT =+ Integer.toHexString(crc));
return crc;
}

和PHP实现

 <?php 
函数crc16($ data)
{
$ crc = 0xFFFF; ($ i = 0; $ i< strlen($ data); $ i ++)
{
$ x =(($ crc>> 8)^ ord data [$ i]))& 0xFF;
$ x ^ = $ x>> 4;
$ crc =(($ crc<< 8)^($ x <12)^($ x << 5)^ $ x)& 0xFFFF;
}
return $ crc;
}


解决方案

  function crc16(Buffer:String; Polynom,Initial:Cardinal):Cardinal; 
var
i,j:整数;
begin
结果:=初始;
for i:= 1 to Length(Buffer)do begin
结果:=结果xor(ord(buffer [i])shl 8);
for j:= 0到7 do begin
if(Result and $ 8000)<> 0 then Result:=(Result shl 1)xor Polynom
else Result:= Result shl 1 ;
结束
结束
结果:= Result和$ ffff;
结束

source: http://www.miscel.dk/MiscEl/CRCcalculations.html


Can someone help me with Delphi implementation of CRC-CCITT (0xFFFF)?

Already get the Java version, but confusing on how to port it to Delphi

public static int CRC16CCITT(byte[] bytes) {
    int crc = 0xFFFF;          // initial value
    int polynomial = 0x1021;   // 0001 0000 0010 0001  (0, 5, 12) 

    for (byte b : bytes) {
        for (int i = 0; i < 8; i++) {
            boolean bit = ((b   >> (7-i) & 1) == 1);
            boolean c15 = ((crc >> 15    & 1) == 1);
            crc <<= 1;
            if (c15 ^ bit) crc ^= polynomial;
         }
    }

    crc &= 0xffff;
    //System.out.println("CRC16-CCITT = " + Integer.toHexString(crc));
    return crc;
}

and for PHP implementation

<?php
function crc16($data)
 {
   $crc = 0xFFFF;
   for ($i = 0; $i < strlen($data); $i++)
   {
     $x = (($crc >> 8) ^ ord($data[$i])) & 0xFF;
     $x ^= $x >> 4;
     $crc = (($crc << 8) ^ ($x << 12) ^ ($x << 5) ^ $x) & 0xFFFF;
   }
   return $crc;
 }

解决方案

i found some code that works:

function crc16(Buffer:String;Polynom,Initial:Cardinal):Cardinal;
var
  i,j: Integer;
begin
Result:=Initial;
for i:=1 to Length(Buffer) do begin
  Result:=Result xor (ord(buffer[i]) shl 8);
  for j:=0 to 7 do begin
    if (Result and $8000)<>0 then Result:=(Result shl 1) xor Polynom
    else Result:=Result shl 1;
    end;
  end;
Result:=Result and $ffff;
end;

source : http://www.miscel.dk/MiscEl/CRCcalculations.html

这篇关于CRC-CCITT(0xFFFF)功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆