ex_crc v0.1.0 ExCRC View Source

Quickly calculate CRC16-CCITT checksum based on a lookup table.

The table is generated with:

def crc_entry(_, crc, 8), do: crc
def crc_entry(c, crc, bc) do
  case (crc ^^^ c) &&& 0x8000 do
    0 -> crc_entry(c <<< 1, crc <<< 1, bc + 1)
    _ -> crc_entry(c <<< 1, (crc <<< 1) ^^^ 0x1021, bc + 1)
  end
end
def crc_table do
  for i <- 0..255, into: %{} do
    crc = 0
    c = i <<< 8
    {i, crc_entry(c, crc, 0) &&& 0xffff}
  end
end

CRC16-CCITT uses an initial value of 0xFFFF and the polynomial 0x1021. There are several variants so it’s important to know which one you want to use. See the CRC catalogue for an exhaustive list. The key parameter in identifying the variant is the check value. That is the CRC of the string 123456789. The variant implemented in this library has check value 0x29b1 (CRC-16/CCITT-FALSE) in the catalogue. This check is included in the tests.

I may add additional variant support as time permits.

Ported to Elixir from Lammert Bies’ libCRC including the tests, which pass.

Link to this section Summary

Functions

Compute and return the CRC16-CCITT checksum of a binary value. Some variants, XMODEM, Kermit, etc., may require a different start value. The start value 65535 (0xffff) is the default

Link to this section Functions

Link to this function crc16(value, start \\ 65535) View Source

Compute and return the CRC16-CCITT checksum of a binary value. Some variants, XMODEM, Kermit, etc., may require a different start value. The start value 65535 (0xffff) is the default.