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?
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.
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))
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.
[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.
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.