# Ascii to binary problem

**URL:** <https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640>\
**Category:** Ignition\
**Created:** [October 20, 2015, 10:41pm UTC](https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640 "2015-10-20T22:41:39Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterSilk01](https://avatars.discourse-cdn.com/v4/letter/p/34f0e0/32.png) [@PeterSilk01](https://forum.inductiveautomation.com/u/PeterSilk01)\
**Post date:** [October 20, 2015, 10:41pm UTC](https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640/1 "2015-10-20T22:41:39Z")

</div>

I hope someone can help me with this would be appreciated!

I have a serial device that brings in a string that represents the device’s status say “04;800002”  
Each character represents 4 bits such that there are 9 characters, each representing 4 states of the device in binary = 36 boolean states I need to extract and create tags for.  
ie the character “;” = 3B(Hex) . Subbing the 30 leaves B = 1011  
I have been trying but find things I need in expressions are only available in scripts or visa versa and its doing my head in.  
I think the boolean array may be the go but have to get the values into two integers first to be able to use it I guess?

Any help is appreciated (my first post!)  
thanks

---

<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:** [October 21, 2015, 10:43am UTC](https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640/2 "2015-10-21T10:43:41Z")

</div>

Hi!

First, welcome to the forums! 🙂

Next, you will need the Math Extensions found [here](https://inductiveautomation.com/forum/viewtopic.php?p=19112#p19112). Jython 2.5 doesn’t support bin(), so I wrote new functions as needed. Import the project there. For safety’s sake, use a fresh project to import into. Once the extensions are imported into the shared scripts area, you should be good to go.

The following script will process the string into a boolean array, just using the low nibble.

[code]#####################################################

# Assuming stringIn is high-byte to-low byte order,

# everything is processed in reverse order.

#####################################################

stringIn = “04;800002”

#initialize dataset for boolean array  
headers=[‘VALUES’]  
dataOut=[]

for character in reversed(stringIn):  
hexValue = hex(ord(character))  
hexLowNibble = hex(ord(character) & 15)  
binLowNibble = shared.math.dec2bin(ord(character) & 15, 4, 0)[0][:-1]  
print character, hexValue, hexLowNibble, binLowNibble  
for bit in reversed(binLowNibble):  
dataOut.append([bit])  
arrayOut = system.dataset.toDataSet(headers,dataOut)  
system.tag.write(“tagName”, arrayOut)[/code]

Hope this helps!

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [October 21, 2015, 2:25pm UTC](https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640/3 "2015-10-21T14:25:47Z")

</div>

Here’s a solution that doesn’t need the maths extensions:[code]stringIn = “04;800002”  
dataOut = []

for character in stringIn:  
for i in range (3,-1,-1):  
dataOut.append(str(1 & (ord(character) & 15) \>\> i))  
print dataOut[/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:** [October 21, 2015, 2:47pm UTC](https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640/4 "2015-10-21T14:47:51Z")

</div>

[quote=“AlThePal”]Here’s a solution that doesn’t need the maths extensions:[code]stringIn = “04;800002”  
dataOut = []

for character in stringIn:  
for i in range (3,-1,-1):  
dataOut.append(str(1 & (ord(character) & 15) \>\> i))  
print dataOut[/code][/quote]

Showoff… 😛

😆

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [October 21, 2015, 2:52pm UTC](https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640/5 "2015-10-21T14:52:05Z")

</div>

![8)](https://us1.discourse-cdn.com/inductiveautomation/original/1X/587d7fe0d6822f36e81815ab06aa7164e9d2e057.gif "Cool") I couldn’t resist it! I love bit wrangling - too much time spent messing with microcontrollers I reckon.

---

<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:** [October 21, 2015, 3:36pm UTC](https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640/6 "2015-10-21T15:36:08Z")

</div>

Hmm, a friendly contest 🙂  
How about a one-liner?`print ''.join([''.join(['%i' % ((ord(c) & (8 >> b))!=0) for b in range(4)]) for c in inputString])`

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [October 21, 2015, 3:48pm UTC](https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640/7 "2015-10-21T15:48:40Z")

</div>

Well it works, but I don’t understand it so it doesn’t count 😉

This only gives you a string which isn’t very useful. I had a go with list comprehensions but I couldn’t get all the values nicely into one list.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [October 21, 2015, 3:57pm UTC](https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640/8 "2015-10-21T15:57:23Z")

</div>

I made the mistake of doing this from the command line, where I discovered too late that bin() is 2.6 and later. Here’s my solution anyway…

[code]data = ‘04;800002’  
nested = map(lambda x: bin(ord(x))[4:], data)  
flatten = lambda z: [x for y in z for x in y]  
bool\_array = map(lambda x: int(x) != 0, flatten(nested))

print(bool\_array)[/code]

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [October 21, 2015, 4:06pm UTC](https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640/9 "2015-10-21T16:06:33Z")

</div>

I think it’s pretty clear from the responses who are the real programmers and who are the engineers trying to get the job done without too much ‘magic’ 🙂

I’ve come to the realisation that while the basics of Python are very easy (which is why it’s used so widely to teach introductions to programming) it rapidly becomes very ‘magical’ indeed and hard for non-specialist programmers to understand.

---

<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:** [October 21, 2015, 4:26pm UTC](https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640/10 "2015-10-21T16:26:21Z")

</div>

[quote=“AlThePal”]This only gives you a string which isn’t very useful[/quote]Yeah. But wrap it in one more list comprehension and you have the list of the numbers 🙂`print [int(x) for x in ''.join([''.join(['%i' % ((ord(c) & (8 >> b))!=0) for b in range(4)]) for c in inputString])]`[quote=“Kevin.Herron”]… I discovered too late that bin() is 2.6 and later.[/quote]Heh. I started with the ‘b’ specifier in python’s format(), which is 2.6 or later. ☹[quote=“AlThePal”]I’ve come to the realisation that while the basics of Python are very easy (which is why it’s used so widely to teach introductions to programming) it rapidly becomes very ‘magical’ indeed and hard for non-specialist programmers to understand.[/quote]Agreed. Unfortunately, the lambdas and list comprehensions are considered the ‘best’ methods by the python developer community, so you see many solutions on the web done this way.

---

<div class="post-metadata">

**Author:** ![PeterSilk01](https://avatars.discourse-cdn.com/v4/letter/p/34f0e0/32.png) [@PeterSilk01](https://forum.inductiveautomation.com/u/PeterSilk01)\
**Post date:** [October 22, 2015, 12:06pm UTC](https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640/11 "2015-10-22T12:06:22Z")

</div>

Wow,  
Seriously blown away by so many clever members who are prepared to share their skills and help each other , Thanks everyone!!, I’ll try the solutions offered tomorrow and see how I go.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [October 22, 2015, 1:21pm UTC](https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640/12 "2015-10-22T13:21:15Z")

</div>

[quote=“PeterSilk01”]Wow,  
Seriously blown away by so many clever members who are prepared to share their skills and help each other , Thanks everyone!!, I’ll try the solutions offered tomorrow and see how I go.[/quote]

You [nerd sniped](https://xkcd.com/356/) a bunch of us with this kind of question. ![:thumb_right:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/2026e28c0c425034352c1b75807edf956adc75f4.gif ":thumb\_right:")

---

<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:** [October 22, 2015, 1:24pm UTC](https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640/13 "2015-10-22T13:24:38Z")

</div>

[quote=“Kevin.Herron”]

You [nerd sniped](https://xkcd.com/356/) a bunch of us with this kind of question. ![:thumb_right:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/2026e28c0c425034352c1b75807edf956adc75f4.gif ":thumb\_right:")[/quote]  
I can’t stop laughing at that one! 😆

---

<div class="post-metadata">

**Author:** ![AutomationNC](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@AutomationNC](https://forum.inductiveautomation.com/u/AutomationNC)\
**Post date:** [October 22, 2015, 7:09pm UTC](https://forum.inductiveautomation.com/t/ascii-to-binary-problem/10640/14 "2015-10-22T19:09:47Z")

</div>

Yeah but I’m still chuckling over the “yeah but i don’t understand it so it doesn’t count”
