class Imb::Crc
Calculates the Intelligent Mail Barcode
CRC.
See spec. section 9.1 (“CRC Generating Code”)
Constants
- BITS_PER_BYTE
- CRC_BITS
- CRC_MSB_MASK
- LEADING_BITS_TO_IGNORE
- MASK
- NUM_INPUT_BYTES
- POLYNOMIAL
Public Instance Methods
crc(binary_data)
click to toggle source
Calculate a CRC. @param binary_data [Integer] A 102-bit integer @return [Integer] An 11-bit CRC
# File lib/usps_intelligent_barcode/crc.rb, line 17 def crc(binary_data) crc = MASK bytes = numeric_to_bytes(binary_data, NUM_INPUT_BYTES) crc = crc_byte(crc, bytes.first, LEADING_BITS_TO_IGNORE) for byte in bytes[1...NUM_INPUT_BYTES] crc = crc_byte(crc, byte, 0) end crc end
Private Instance Methods
crc_byte(crc, byte, leading_bits_to_ignore)
click to toggle source
# File lib/usps_intelligent_barcode/crc.rb, line 37 def crc_byte(crc, byte, leading_bits_to_ignore) num_bits = BITS_PER_BYTE - leading_bits_to_ignore data = byte << CRC_BITS - BITS_PER_BYTE + leading_bits_to_ignore num_bits.times do use_polynomial = (crc ^ data) & CRC_MSB_MASK crc <<= 1 crc ^= POLYNOMIAL if use_polynomial != 0 crc &= MASK data <<= 1 end crc end