老师,
条码:6901849257652
380eee13363930313834393235373635320a
CRC16func(str) :EE13
ret.characteristic.value.substring(4, 8)).toUpperCase() :EE13
这样可以相等!
但下面的这个就不行
条码:6932394905163
380e0944363933323339343930353136330a
CRC16func(str) :944
ret.characteristic.value.substring(4, 8)).toUpperCase() :0944
他自动把 0 给去掉了,那怎么处理呢?
甜老师给的代码如下:
function CRC16func(AllDATA) {
var cnCRC_CCITT = 0x1021;
var i = -1,
j = -1;
var nData = -1,
nAccum = -1,
nYAccum = -1;
var Table_CRC = [];
var aSize = AllDATA.length;
for (i = 0; i < 256; i++) {
nData = C16func.call(this, i, 8);
nAccum = 0;
for (j = 0; j < 8; j++) {
if ((nData ^ nAccum) & 0x8000) {
nAccum = C16func.call(this, nAccum, 1);
nAccum = nAccum ^ cnCRC_CCITT;
} else {
nAccum = C16func.call(this, nAccum, 1);
nAccum = nAccum;
}
nData = C16func.call(this, nData, 1);
}
Table_CRC[i] = nAccum;
}
nAccum = 0;
for (i = 0; i < aSize; i++) {
nYAccum = nAccum;
nAccum = C16func.call(this, nAccum, 8);
nAccum = nAccum ^ Table_CRC[(nYAccum >>> 8) ^ (AllDATA.charCodeAt(i))];
}
return nAccum.toString(16).toUpperCase();
}
function C16func(cData, cLen) {
if (((cData << cLen).toString(2).length > 16) && (cData.toString(2).length <= 16)) {
cData = (cData << cLen).toString(2).substr((cData << cLen).toString(2).length - 16, 16);
cData = parseInt(cData, 2);
} else {
cData = cData << cLen;
}
return cData;
}
[此贴子已经被作者于2017/10/15 22:47:59编辑过]