CRC-CCITT (0xFFFF) 功能?
有人可以帮助我用 Delphi 实现 CRC-CCITT (0xFFFF
) 吗?
已经获得了 Java 版本,但对如何将其移植到 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;
}
以及 PHP 实现感到困惑
<?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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
0xFFFF
转换为$FFFF
&
转换为且
^
转换为xor
<<
转换为shl
>>
转换为shr
x ^= y
转换为x := x xor y
,类似于&=
、<<=
等。这些运算符在 Delphi 中通常具有较高的优先级,因此它们通常需要将其参数放在括号中。
我很确定 Delphi 还有很多 CRC16 等的其他实现,请参见 Improve Crc16 计算速度
0xFFFF
translates to$FFFF
&
translates toand
^
translates toxor
<<
translates toshl
>>
translates toshr
x ^= y
translates tox := x xor y
, similar for&=
,<<=
, etc.These operators generally have higher precedence in Delphi so they usually need to have their arguments parenthesized.
I'm quite sure that there are plenty of other implementations of CRC16 etc. for Delphi, see e.g. Improve speed on Crc16 calculation
您可以在 Delphi Encryption Compendium (DEC) 组件中找到一个。
http://blog.digivendo.com/2008/11/delphi-encryption-compendium-dec-52-for-d2009-released/
You can find one in Delphi Encryption Compendium (DEC) component.
http://blog.digivendo.com/2008/11/delphi-encryption-compendium-dec-52-for-d2009-released/
我发现一些有效的代码:
来源:http://www.miscel.dk/MiscEl/CRCcalculations。 html
i found some code that works:
source : http://www.miscel.dk/MiscEl/CRCcalculations.html