I try to re-write CCNET driver for CashCode, from node to Python. But, i realy can`t run CRC generator.
You can find "working" code on Github repo
Here is the JS function:
function getCRC16(bufData) {
var POLYNOMIAL = 0x08408;
var sizeData = bufData.length;
var CRC, i, j;
CRC = 0;
for (i = 0; i < sizeData; i++) {
CRC ^= bufData[i];
for (j = 0; j < 8; j++) {
if (CRC & 0x0001) {
CRC >>= 1;
CRC ^= POLYNOMIAL;
} else CRC >>= 1;
}
}
var buf = new Buffer(2);
buf.writeUInt16BE(CRC, 0);
CRC = buf;
return Array.prototype.reverse.call(CRC);
}
I try crcmod
, BUT it`s not predefined function, and when i try set polynominal, get error
Here is my sometime working code:
@staticmethod
def getCRC16(data):
CRC = 0
for i in range(0, len(data), 2):
CRC ^= int(str(data[i:(i+2)]), 16)
for j in range(8):
if (CRC & 0x0001):
CRC >>= 1
CRC ^= 0x8408
else:
CRC >>= 1
CRC = format(CRC, '02x')
return CRC[2:4] + CRC[0:2]
And i get
CRC ^= int(str(data[i:(i+2)]), 16)
ValueError: invalid literal for int() with base 16: '\x02\x03'
Help me with that function. (input binary/integers or HEX-strings)
UPD: : It works with bytearray.fromhex(data)
. Thanks)
@staticmethod
def getCRC16(data):
data = bytearray.fromhex(data)
CRC = 0
for bit in data:
CRC ^= bit
for j in range(0, 8):
if (CRC & 0x0001):
CRC >>= 1
CRC ^= 0x8408
else:
CRC >>= 1
CRC = format(CRC, '02x')
return CRC[2:4] + CRC[0:2]
Sinse Python 2.6: bytearray.fromhex(data)
.
E.G.
for byte in bytearray.fromhex(data):
CRC ^= byte
...