把这个JAVA函数或则C函数改写成DELPHI的。该如何处理
把这个JAVA函数或则C函数改写成DELPHI的。。急~~
需要一个DELPHI版的CRC验证码函数,大家帮我改改。。急~~·
java语言:
public static int getCRC(String buf) {
int crc = 0xFFFF; // initial value
int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
for (int j = 0; j < buf.length(); j++) {
char b = buf.charAt(j);
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;
return crc;
}
C语言:
typedef unsigned short uint16_t;
typedef unsigned char BYTE;
uint16_t crc16_ccitt(BYTE *ucbuf, int iLen)
{
uint16_t crc = 0xFFFF; // initial value
uint16_t polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
for (int j = 0; j < iLen; ++j) {
for (int i = 0; i < 8; i++) {
char bit = ((ucbuf[j] >> (7-i) & 1) == 1);
char c15 = ((crc >> 15 & 1) == 1);
crc <<= 1;
if (c15 ^ bit) crc ^= polynomial;
}
}
crc &= 0xffff;
return crc;
}
------解决方案--------------------
byte * PByte,其他都是基本的位运算,都是一样的
需要一个DELPHI版的CRC验证码函数,大家帮我改改。。急~~·
java语言:
public static int getCRC(String buf) {
int crc = 0xFFFF; // initial value
int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
for (int j = 0; j < buf.length(); j++) {
char b = buf.charAt(j);
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;
return crc;
}
C语言:
typedef unsigned short uint16_t;
typedef unsigned char BYTE;
uint16_t crc16_ccitt(BYTE *ucbuf, int iLen)
{
uint16_t crc = 0xFFFF; // initial value
uint16_t polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
for (int j = 0; j < iLen; ++j) {
for (int i = 0; i < 8; i++) {
char bit = ((ucbuf[j] >> (7-i) & 1) == 1);
char c15 = ((crc >> 15 & 1) == 1);
crc <<= 1;
if (c15 ^ bit) crc ^= polynomial;
}
}
crc &= 0xffff;
return crc;
}
------解决方案--------------------
byte * PByte,其他都是基本的位运算,都是一样的