0

I created a list of size 100 and populated the array with 8bit data in python using the following code and I want to calculate the CRC value using the zlib.crc32() function.

Init_RangenCrc8 = []
for i in range(0,100):
    Init_RangenCrc8.append(random.randrange(0, 255, 1))

crc8_python = zlib.crc32(Init_RangenCrc8, 0xFFFF) 

When I return and print the crc8_python, I am not getting any value back.

Any help would be appreciated, thanks.

1
  • Your code looks a little strange. Init_RangenCrc8 = [] should be before the for loop, shouldn't it? The indentation doesn't look right. Commented Apr 30, 2013 at 5:57

1 Answer 1

7
>>> help(zlib.crc32)
Help on built-in function crc32 in module zlib:

crc32(...)
    crc32(string[, start]) -- Compute a CRC-32 checksum of string.

    An optional starting value can be specified.  The returned checksum is
    a signed integer.
>>> zlib.crc32("".join(chr(random.randrange(0,255)) for _ in xrange(100)))
333158331

EDIT: code that uses the start value 0xFFFF:

>>> text = "".join(chr(random.randrange(0,255)) for _ in xrange(100))

>>> zlib.crc32(text)
-964269250

>>> zlib.crc32(text, 0xFFFF)
2057263175
Sign up to request clarification or add additional context in comments.

4 Comments

You've missed the 0xFFFF start value. Nevertheless +1.
What error are you getting? Also, do you want a crc32 for each element of the list, or a single crc32 for the string comprised of each char in the list?
The first argument has to be a string because that is how the API is defined. As far as the error, you need to import zlib and import random for this solution to work, which is the right solution given your description.
@Chris Row; you've got to import zlib first to use the contents of the module. Also "why does the first argument have to be a string?" -- that's what the documentation says the function takes. If it's documented to take a data type, it's probably not going to work correctly if you give it an input of a different type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.