# CRC 16 0x1021

I want to generate CRC numbers for a data string, The CRC used is a 16 bit CRC using the standard 0x1021 polynomial. The initial value of the CRC is 0xFFFF. can anyone assist if code available please?

We do not have CRC library or specifc samples for that. I however did a rapid search on the itnernet and found github.com/AIE-Guild/GreenWall/ … -CCITT.lua.

You probably inspire yourself from it to have a solution working?

Please note that the bit lib is called bit32, so you’d have to do a
local bit=require ‘bit32’ to have bitwise operation (it is not loaded by default)

The important piece is really that function:

``````function Hash(s)
assert(type(s) == 'string')
local crc = 0xffff
for i = 1, #s do
local c = s:byte(i)
crc = bxor(crc, c)
for j = 1, 8 do
local k = band(crc, 1)
crc = rshift(crc, 1)
if k ~= 0 then
crc = bxor(crc, 0x8408)
end
end
end
return crc
end``````

not sure where I am going wrong with the CRC, i have tried several ways including yours now and still cant get the right answer:
function process_crc(s)
assert(type(s) == ‘string’)
local crc = 0xffff
for i = 1, #s do
local c = s:byte(i)
crc = bit_xor(crc, c)
for j = 1, 8 do
local k = bit_and(crc, 1)
crc = bit_rshift(crc, 1)
if k ~= 0 then
crc = bit_xor(crc, 0x8408)
end
end
end
print(“CRC:”, crc)
return DEC_HEX(crc)
end

print(“CRC Return”, process_crc(“123456789”))

I am getting 0x6F91 while the right answer is 0xE5CC

The code I provided was an example, I not sure how exactly it fits you’re algorithm. You’d need to look into the algorithm to understand.
For instance I think that the algorithm I provided does not have a different polynom (0x8408 instead of 0x1021). The example provides you the low level bitwise operator API. I would recommend to implement the correct CRC algorithm you need with these primitives and the algorithm that should be available on the internet.
Sorry I do not have a readily usable algorithm at hand.
let me know how it goes.

Your problem got me curious so I digged a little bit more in CRC16. It happens that there is no one CRC16-CCITT, but plenty of them, really. Look at github.com/blackbeam/rust-crc16 for CRC16 with different configuration.
From you comment above, expecting 0xE5CC for the input “123456789” I figured that what you were looking for was actually that configuration:

I modified the above algorith to have those parameters, and that give this code:

``````local bit = require'bit32'
local bxor, band, rshift, lshift = bit.bxor, bit.band, bit.rshift, bit.lshift

function hash(s)
assert(type(s) == 'string')
local crc = 0x1d0f
for i = 1, #s do
local c = s:byte(i)
crc = bxor(crc, lshift(c, 8))
for j = 1, 8 do
local k = band(crc, 0x8000) == 0x8000
crc = lshift(crc, 1)
if k then
crc = bxor(crc, 0x1021)
end
crc = band(crc, 0xFFFF)
end
end
return crc
end

print(hash("123456789"))``````

thank you for the follow up. the string that i am using is for example 00B0470010a70028 so i am reading every two characters together and changing tonumber. the good result of this one is 0xE7DA according to CRC-CCITT (0xFFFF) on lammertbies.nl/comm/info/cr … ation.html but i am getting DE7C

function hash(s)
assert(type(s) == ‘string’)
local crc = 0x1021
for i = 1, #s/2, 2 do
local c = string.byte(tonumber(string.sub(s, i, i+1),16))
crc = bxor(crc, lshift(c, 8))
for j = 1, 8 do
local k = band(crc, 0x8000) == 0x8000
crc = lshift(crc, 1)
if k then
crc = bxor(crc, 0x1021)
end
crc = band(crc, 0xFFFF)
end
end
return crc

i removed the /2 from “for i = 1, #s/2, 2 do” and used the 0xffff as initial value instead of (0x1021) but still not working

I am sorry I cannot help you more. As I mentioned before there are many different configurations for CRC16 as described here: github.com/blackbeam/rust-crc16
I provided the code sample for two different configurations.It maybe interesting for you to get in touch with our Professional Services Team, they could develop a library and/or an AAF application that fits your requirements. I have worked with them before and I know they are very talented and efficient. Please contact your FSE if you need more information on this.

i think i just found a solution for this one. if not I will contact the dev team