Ascii to binary problem

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

Hi!

First, welcome to the forums! :slight_smile:

Next, you will need the Math Extensions found here. 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!

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=“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… :stuck_out_tongue:

:laughing:

8) I couldn’t resist it! I love bit wrangling - too much time spent messing with microcontrollers I reckon.

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

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

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.

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]

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’ :slight_smile:

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=“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 :slight_smile: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. :frowning:[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.

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=“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 a bunch of us with this kind of question. :thumb_right:

[quote=“Kevin.Herron”]

You nerd sniped a bunch of us with this kind of question. :thumb_right:[/quote]
I can’t stop laughing at that one! :laughing:

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