# CRC "Cyclic Redundancy Check"

**URL:** <https://forum.inductiveautomation.com/t/crc-cyclic-redundancy-check/78902>\
**Category:** Ignition\
**Created:** [September 8, 2023, 10:07am UTC](https://forum.inductiveautomation.com/t/crc-cyclic-redundancy-check/78902 "2023-09-08T10:07:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hamza\_DAZARA](https://avatars.discourse-cdn.com/v4/letter/h/dec6dc/32.png) [@Hamza\_DAZARA](https://forum.inductiveautomation.com/u/Hamza_DAZARA)\
**Post date:** [September 8, 2023, 10:07am UTC](https://forum.inductiveautomation.com/t/crc-cyclic-redundancy-check/78902/1 "2023-09-08T10:07:06Z")

</div>

Is it possible to generate a CRC (cyclic redundancy check) in ignition ?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [September 8, 2023, 10:15am UTC](https://forum.inductiveautomation.com/t/crc-cyclic-redundancy-check/78902/2 "2023-09-08T10:15:43Z")

</div>

If it's possible in Python 2.7 then it's possible in Ignition.  
A web search shows that it is possible in Python 2.7. Try this (but I haven't):

> **[pycrc, a free CRC source code generator](https://pycrc.org/index.html)**
>
> pycrc is a free CRC calculator and C source code generator written in Python.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 8, 2023, 11:50am UTC](https://forum.inductiveautomation.com/t/crc-cyclic-redundancy-check/78902/3 "2023-09-08T11:50:41Z")

</div>

It's built into Java: [java.util.zip.CRC32](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/zip/CRC32.html)

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 1, 2023, 10:33am UTC](https://forum.inductiveautomation.com/t/crc-cyclic-redundancy-check/78902/4 "2023-11-01T10:33:58Z")

</div>

Hi, Phil.  
Could you spare a few lines on how to set this up? I've never done it.

I have recipes in a table and I'd like to provide a CRC of the recipe parameters (for each recipe) so that the recipe can be checked by a single number rather than a parameter by parameter cross-check. The recipes are returned from the database in a (typically) 8 x 14 dataset.

Thanks.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 1, 2023, 11:00am UTC](https://forum.inductiveautomation.com/t/crc-cyclic-redundancy-check/78902/5 "2023-11-01T11:00:09Z")

</div>

So, simple usage would look like this:

```auto
from java.util.zip import CRC32

def crcOfBytes(bytes):
	crc = CRC32()
	crc.update(bytes)
	return crc.value

```

More complex cases just need to call `.update()` more, and perform conversions of your actual object types to individual bytes, byte arrays, or byte buffers. Whatever method you choose, your consistency in ordering of operations will be critical to repeatable CRC values.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 1, 2023, 4:34pm UTC](https://forum.inductiveautomation.com/t/crc-cyclic-redundancy-check/78902/6 "2023-11-01T16:34:19Z")

</div>

> [@pturmel](#):
>
> `update`

That was beautifully simple, thanks, Phil, and saved me a lot of time.

For anyone else, I want the CRC to be easily human readable so I've converted the output to hex with a space between the first four and second four characters.

```auto
def crc(data):
	from java.util.zip import CRC32
	
	def crcOfBytes(bytes):
		crc = CRC32()
		crc.update(bytes)
		return crc.value
		
	dataBytes = bytes(data)
	crc = "0x{:08x}".format(crcOfBytes(dataBytes))	# Force leading zeros.
	return crc[2:6] + " " + crc[6:10] # return format "1a2b 3c4d".

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 1, 2023, 4:38pm UTC](https://forum.inductiveautomation.com/t/crc-cyclic-redundancy-check/78902/7 "2023-11-01T16:38:09Z")

</div>

Ewww! Why did you nest the `def`s? Just make a library script that does the whole thing the way you want.

Except for simple closures for callbacks, `def` doesn't belong in events or transforms. And `import` not at all.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 1, 2023, 4:51pm UTC](https://forum.inductiveautomation.com/t/crc-cyclic-redundancy-check/78902/8 "2023-11-01T16:51:29Z")

</div>

If you're on 8.1.33 (or higher), I'd also suggest pulling in [`HexFormat`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/HexFormat.html) (in another project library constant, naturally 🙂) to do the actual hex output.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 2, 2023, 9:16am UTC](https://forum.inductiveautomation.com/t/crc-cyclic-redundancy-check/78902/9 "2023-11-02T09:16:35Z")

</div>

> [@pturmel](#):
>
> Ewww!

You're right. I wasn't thinking straight at all. How's this:

```auto
from java.util.zip import CRC32
def crc(data):
	crc32 = CRC32()
	crc32.update(bytes(data))
	
	checksum = "0x{:08x}".format(crc32.value) # Force leading zeros.
	return checksum[2:6] + " " + checksum[6:10] # return format "1a2b 3c4d".

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 2, 2023, 11:58am UTC](https://forum.inductiveautomation.com/t/crc-cyclic-redundancy-check/78902/10 "2023-11-02T11:58:21Z")

</div>

> [@Transistor](#):
>
> How's this:

Better. But there's no reason for the import to be inside the function. Put it outside.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 2, 2023, 12:37pm UTC](https://forum.inductiveautomation.com/t/crc-cyclic-redundancy-check/78902/11 "2023-11-02T12:37:10Z")

</div>

Done!  
Thanks for the education. I'm a chancer at Python.
