# Scripting equivalent to binEnc()

**URL:** <https://forum.inductiveautomation.com/t/scripting-equivalent-to-binenc/4212>\
**Category:** Ignition\
**Created:** [March 27, 2012, 2:30am UTC](https://forum.inductiveautomation.com/t/scripting-equivalent-to-binenc/4212 "2012-03-27T02:30:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nate.Olson](https://avatars.discourse-cdn.com/v4/letter/n/35a633/32.png) [@Nate.Olson](https://forum.inductiveautomation.com/u/Nate.Olson)\
**Post date:** [March 27, 2012, 2:30am UTC](https://forum.inductiveautomation.com/t/scripting-equivalent-to-binenc/4212/1 "2012-03-27T02:30:39Z")

</div>

Is there an equivalent to binEnc() in the scripting environment?

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [March 27, 2012, 3:44pm UTC](https://forum.inductiveautomation.com/t/scripting-equivalent-to-binenc/4212/2 "2012-03-27T15:44:32Z")

</div>

No, but you can use the following function:[code]def binEnc(args):  
value = 0  
for i in range(len(args)):  
if args[i]:  
value |= 1 \<\< i  
return value

print binEnc([1, 0, 1, 0, 1])[/code]

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 27, 2012, 4:03pm UTC](https://forum.inductiveautomation.com/t/scripting-equivalent-to-binenc/4212/3 "2012-03-27T16:03:23Z")

</div>

Travis posted before I got mine, which was really close! 😆

Variation on a theme…

[code]def binEnc(\*args):  
value = 0  
for i in range(len(args)):  
if args[i]:  
value |= 1 \<\< i  
return value

print binEnc(1, 0, 1, 0, 1)[/code]

The only difference is that with \*args, making a list is not needed. Both have their uses, since you may find it better to create a list via some other routine and binEnc that. If it’s something that stays static, then the second one may be okay for you.

---

<div class="post-metadata">

**Author:** ![Nate.Olson](https://avatars.discourse-cdn.com/v4/letter/n/35a633/32.png) [@Nate.Olson](https://forum.inductiveautomation.com/u/Nate.Olson)\
**Post date:** [March 27, 2012, 9:21pm UTC](https://forum.inductiveautomation.com/t/scripting-equivalent-to-binenc/4212/4 "2012-03-27T21:21:44Z")

</div>

I’ll test these out. Thanks guys.
