This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KillaVanilla's profile picture

Cyclic Redundancy Checks (Aka Crc)

Started by KillaVanilla, 24 January 2013 - 10:22 AM
KillaVanilla #1
Posted 24 January 2013 - 11:22 AM
Hello again.

I've been playing with DCPU-16 assembly for quite some time now, and after writing a checksum calculator for the DCPU-16, I've decided to write something similar in Lua.
This API simply provides a means to calculate CRC checksums with user-specified generator polynomials, and also includes functions to convert to/from hexadecimal and decimal.

For best results, use the calculate_crc function with the constants 0x04C11DB7 or 0x1EDC6F41. Note: Not a secure or suitable replacement for secure hashes such as SHA-1 or SHA-2.

Get it here.
NeverCast #2
Posted 24 January 2013 - 11:30 AM
This is fantastic!
And like any CRC Algorithm needs to be, it looks like it will be fast.

I do suggest using

for i=1, #data do
  local v = data[i]

Simply because ipairs will be retired in Lua 5.2, and although there is no indication yet that CC will be changing. It might.
That and an incremental for loop is faster than a pairs loop :P/>

Thanks for the submission.
KillaVanilla #3
Posted 24 January 2013 - 03:36 PM
This is fantastic!
And like any CRC Algorithm needs to be, it looks like it will be fast.

I do suggest using

for i=1, #data do
  local v = data[i]

Simply because ipairs will be retired in Lua 5.2, and although there is no indication yet that CC will be changing. It might.
That and an incremental for loop is faster than a pairs loop :P/>

Thanks for the submission.
They're retiring ipairs()? Wow. Then again, I can see why one would use incremental loops vs. ipairs/pairs. Function overhead and all that. Anyways, thanks for the tip.
NeverCast #4
Posted 24 January 2013 - 03:42 PM
You're welcome :)/>
Thanks for the CRC code.
Xtansia #5
Posted 24 January 2013 - 11:28 PM
Just some small things:

function intToHex(input)
    return string.format("%X", input)
end

function hexToInt(input)
    return tonumber(input, 16)
end
KillaVanilla #6
Posted 25 January 2013 - 01:25 AM
Just some small things:

function intToHex(input)
	return string.format("%X", input)
end

function hexToInt(input)
	return tonumber(input, 16)
end

Heh. I forgot about string.format and I didn't know that tonumber took a base as well.
Imgoodisher #7
Posted 06 February 2013 - 12:07 PM
This is fantastic!
And like any CRC Algorithm needs to be, it looks like it will be fast.

I do suggest using

for i=1, #data do
  local v = data[i]

Simply because ipairs will be retired in Lua 5.2, and although there is no indication yet that CC will be changing. It might.
That and an incremental for loop is faster than a pairs loop :P/>/>/>/>/>/>

Thanks for the submission.

Are you sure about that? I see both 'pairs' and 'ipairs' in the Lua 5.2 documentation and nothing about it being removed/deprecated in the 'Incompatibilities with the Previous Version' section

Not to mention that that code wont work if

data = {"a", "b", nil, "c"}
(it would loop 2 times, not 3)
MysticT #8
Posted 06 February 2013 - 12:26 PM
Not to mention that that code wont work if

data = {"a", "b", nil, "c"}
(it would loop 2 times, not 3)
Well, that would happen with ipairs too. Not with pairs, but it doesn't iterate in order, so you could get "c", "a", "b" (from your example) or anything.
KillaVanilla #9
Posted 10 February 2013 - 07:24 PM
Are you sure about that? I see both 'pairs' and 'ipairs' in the Lua 5.2 documentation and nothing about it being removed/deprecated in the 'Incompatibilities with the Previous Version' section

Not to mention that that code wont work if

data = {"a", "b", nil, "c"}
(it would loop 2 times, not 3)

Note to self: replace #[table] with table.maxn( [table] ) and conditionals