11-16-2014 09:05 PM
I am trying to simply extract portions of a U16 Hex number and have found a way but there has got to be a VI that I am missing to do it simpler. Anbody know how to do this cleaner?
11-16-2014 10:21 PM
First, I'm assuming you want to recover the Hex digits, i.e. if your input (in Hex) is 4c6, you want to recover 4, 12 (=C in hex), 6. In your code, you transform the number to bits, then take the bits 5 at a time and reassemble them, getting 1, 6, 6. However, a Hex digit is 4, not 5, bits -- if you'd index by multiples of 4, you would have gotten 4, 12, 6.
However, you can also solve this without having to resort to bit operations. Do you know about Integer Division, the square-box function with a Division sign and two outputs, labelled R and IQ (for Integer Quotient)? If you divide a U16 by 16 (or 2^4), your remainder will be the least significant byte (or Hex), and the Quotient will be the upper bytes, shifted down. By putting this operation in a While Loop with a Shift register bringing the dividend in from the left, passing the quotient to the output shift register, and bringing the remainder to an Index tunnel (the one that makes an Array), you can get all of the hex digits very neatly. Note that you stop the loop when the quotient = 0. If you give this loop 4C6, you'll get out the array 6, 12, 4 (least-significant first) -- if you need the other order (4, 12, 6), a simple Array Reverse fixes this. Note that using a While loop will give you only as many digits "as you need" -- an input of, say, 2 with give you an array of size 1. If you desire to always have a 4-element array (corresponding to the 4 hex digits possible in a U16), you can simply replace the While loop with a For loop and sent the Index count to 4.
You should really try this for yourself (which is why I'm not attaching a VI or Snippet) -- play around with this and you'll enjoy seeing the results.
Bob Schor