diff --git a/ext/digest/crc16_dnp/crc16_dnp.c b/ext/digest/crc16_dnp/crc16_dnp.c index 240e62c..0d1db04 100644 --- a/ext/digest/crc16_dnp/crc16_dnp.c +++ b/ext/digest/crc16_dnp/crc16_dnp.c @@ -46,7 +46,7 @@ crc16_t crc16_dnp_update(crc16_t crc, const void *data, size_t data_len) while (data_len--) { tbl_idx = (crc ^ *d) & 0xff; - crc = (crc << 8) ^ crc16_dnp_table[tbl_idx]; + crc = (crc >> 8) ^ crc16_dnp_table[tbl_idx]; d++; } diff --git a/lib/digest/crc16_dnp.rb b/lib/digest/crc16_dnp.rb index e5149a9..1bc6d22 100644 --- a/lib/digest/crc16_dnp.rb +++ b/lib/digest/crc16_dnp.rb @@ -8,6 +8,8 @@ class CRC16DNP < CRC16 INIT_CRC = 0 + XOR_MASK = 0xffff + TABLE = [ 0x0000, 0x365e, 0x6cbc, 0x5ae2, 0xd978, 0xef26, 0xb5c4, 0x839a, 0xff89, 0xc9d7, 0x9335, 0xa56b, 0x26f1, 0x10af, 0x4a4d, 0x7c13, @@ -57,10 +59,6 @@ def update(data) return self end - def finish - self.class.pack(~@crc) - end - end end diff --git a/spec/crc16_dnp_spec.rb b/spec/crc16_dnp_spec.rb new file mode 100644 index 0000000..012d5cd --- /dev/null +++ b/spec/crc16_dnp_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' +require 'crc_examples' +require 'digest/crc16_dnp' + +describe Digest::CRC16DNP do + let(:string) { '1234567890' } + let(:expected) { 'bc1b' } + + it_should_behave_like "CRC" +end