01-19-2011 10:55 AM
I have a Radio Button Control set to allow No Selection and that has 10 radio buttons each representing a position of a switch. I am using a Radio Button Control because at most only one switch position can be active at a time.
I want to convert the selection to a hex value. For example, if radio button1 is selected, then the value is 0x1. If radio button 10 is selected then 0x20.
So far I have code checking to see if the value of the Radio Button Control is 0 (=no selection) or not. If there is a selection I am using a Rotate. Is there another way to do this that is more efficient?
Solved! Go to Solution.
01-19-2011 11:06 AM - edited 01-19-2011 11:15 AM
Create an array constant of your hex values (I guess just U8 with the display set to hexadecimal, or whatever datatype you want, string, etc.).
The first element is "no selection" and so on.
Now use "index array" and wire the radiobutton control to the index terminal. Wire the array constant to the array input.
The output of "index array" will be your desired value.
01-19-2011 11:18 AM
Thank you. Sometimes, lookup tables are the way to go.
01-19-2011 02:38 PM
I had the very identical need a while back.
I offer another way to "skin the cat".
The only other case is passing the zero straight though.
01-19-2011 02:49 PM
Ah! I like this solution too.
I wasn't sure whether using the Rotate is more processor-intensive than some other method I hadn't thought of before.
01-19-2011 03:52 PM - edited 01-19-2011 03:53 PM
I wasn't quite sure how regular your pattern is and what output data you want, thus the suggestion for a LUT. (HEX is too generic a description ;))
I don't understand the need for rotate, shouldn't a shift be more appropriate (or simply scale by powers of two, which is internally also just a shift in the case of integers)
Here's what I would do.
01-19-2011 06:27 PM
I thought of the scale by powers of two as well.
I will use the Shift instead of the Rotate since I won't be needing that capability with the case statement as you have it.
01-19-2011 06:51 PM
The "shift" and "scale by power of two" are identical and fully interchangeable for integer inputs. Pick one. 😉
I think in general you need the case statement if you allow "no selection" for the radiobutton. Please double-check, maybe you are lucky and it gets masked off because you ignore certain higher bits. 😉
01-19-2011 07:28 PM
Yes, I do need the Case statement.
I learned something new.