# Jython bin() Not Working

**URL:** <https://forum.inductiveautomation.com/t/jython-bin-not-working/16542>\
**Category:** Ignition\
**Created:** [January 10, 2018, 3:30pm UTC](https://forum.inductiveautomation.com/t/jython-bin-not-working/16542 "2018-01-10T15:30:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Curlyandshemp](https://avatars.discourse-cdn.com/v4/letter/c/bcef8e/32.png) [@Curlyandshemp](https://forum.inductiveautomation.com/u/Curlyandshemp)\
**Post date:** [January 10, 2018, 3:30pm UTC](https://forum.inductiveautomation.com/t/jython-bin-not-working/16542/1 "2018-01-10T15:30:20Z")

</div>

> <https://stackoverflow.com/questions/10411085/converting-integer-to-binary-in-python>

in need to use scripting to do bit manipulation from a DINT  
I cannot get the jython bin() to work. Is there another version of this in scripting?

---

<div class="post-metadata">

**Author:** ![jpark](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@jpark](https://forum.inductiveautomation.com/u/jpark)\
**Post date:** [January 10, 2018, 3:43pm UTC](https://forum.inductiveautomation.com/t/jython-bin-not-working/16542/2 "2018-01-10T15:43:25Z")

</div>

```auto
from java.lang import Integer

print Integer.toBinaryString(28)

```

---

<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:** [January 10, 2018, 3:49pm UTC](https://forum.inductiveautomation.com/t/jython-bin-not-working/16542/3 "2018-01-10T15:49:57Z")

</div>

Are you actually using the string? If you are just testing or manipulating the bits, jython’s & and | operators will be much more efficient.

---

<div class="post-metadata">

**Author:** ![Curlyandshemp](https://avatars.discourse-cdn.com/v4/letter/c/bcef8e/32.png) [@Curlyandshemp](https://forum.inductiveautomation.com/u/Curlyandshemp)\
**Post date:** [January 10, 2018, 4:06pm UTC](https://forum.inductiveautomation.com/t/jython-bin-not-working/16542/4 "2018-01-10T16:06:37Z")

</div>

What I am doing is using Ignition to maintain and program ‘pin charts’ that will be downloaded to a PLC.  
The pin charts contains an array of [150,3] DINTs stored in a SQL database table  
I use a table component to display the SQL table, then select a row and column to edit.  
I need to capture the DINT, display and manipulate bits and store the DINT back into the database table.

The expression getbit() works for displaying, but there is no way to write a bit into an OPC Tag that is internal.

---

<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:** [January 10, 2018, 4:17pm UTC](https://forum.inductiveautomation.com/t/jython-bin-not-working/16542/5 "2018-01-10T16:17:54Z")

</div>

> [@Curlyandshemp](#):
>
> there is no way to write a bit into an OPC Tag that is internal

Correct. But jython bitwise math can do it for you:

```auto
# Set bit 23 and reset bit 20 of a dint
tagpath = '[default]Some/Path/To/Dint'
i32 = system.tag.read(tagpath).value
i32 = i32 | 0x00800000
# or i32 = i32 | (1 << 23)
i32 = i32 & (~ 0x00100000)
system.tag.write(tagpath, i32)

```

---

<div class="post-metadata">

**Author:** ![Curlyandshemp](https://avatars.discourse-cdn.com/v4/letter/c/bcef8e/32.png) [@Curlyandshemp](https://forum.inductiveautomation.com/u/Curlyandshemp)\
**Post date:** [January 10, 2018, 4:59pm UTC](https://forum.inductiveautomation.com/t/jython-bin-not-working/16542/6 "2018-01-10T16:59:27Z")

</div>

Fantastic, you gave me the right path to follow:  
I have toggle script to a momentary button and custom properties set up to show status of bit:

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/a/abab2b416d2bac9666d0c7d48b7b8e2487b5d3a0.png)

![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/4/4f972c7552d59b099ba2eb8b8883bb25141750b2.png)

---

<div class="post-metadata">

**Author:** ![Curlyandshemp](https://avatars.discourse-cdn.com/v4/letter/c/bcef8e/32.png) [@Curlyandshemp](https://forum.inductiveautomation.com/u/Curlyandshemp)\
**Post date:** [January 10, 2018, 8:18pm UTC](https://forum.inductiveautomation.com/t/jython-bin-not-working/16542/7 "2018-01-10T20:18:46Z")

</div>

This is what I am trying to do, have it working so far, just need button and scripting to get manipulated bits back into data base table:

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/f/f57d792f9a6522a4b9474049460f5fa5761afaed.png)

---

<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:** [January 10, 2018, 8:34pm UTC](https://forum.inductiveautomation.com/t/jython-bin-not-working/16542/8 "2018-01-10T20:34:32Z")

</div>

That’s a sweet UI !

{ dummy text for min length }

---

<div class="post-metadata">

**Author:** ![jpark](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@jpark](https://forum.inductiveautomation.com/u/jpark)\
**Post date:** [January 11, 2018, 4:33pm UTC](https://forum.inductiveautomation.com/t/jython-bin-not-working/16542/9 "2018-01-11T16:33:16Z")

</div>

[Bits.proj](https://us1.discourse-cdn.com/inductiveautomation/original/2X/4/45ad45719e805d7f2812f173b390fd45fbb1dfb2.proj) (33.8 KB)

Create a custom property called OutValue with the expression binEnc(bit0, bit1, bit2, …)  
Then add scripting to update the value in the table.

```auto
if event.propertyName == "OutValue":
	if event.newValue != event.source.MyValue:
		data = event.source.parent.getComponent('Power Table').data
		selectedRow = event.source.parent.getComponent('Power Table').selectedRow
		data = system.dataset.setValue(data,selectedRow,"outs",event.newValue)
		event.source.parent.getComponent('Power Table').data = data

```
