03-31-2009 03:43 PM
I would like to condense an array. The objective here is to take an array that contains 2 or more elements and condense it into one element. For example, my 1-D array contains the elements 1000, 11, 0, 1. I would like to condense it into the number 10001101. I've come at this from number of directions, but have had problems. I know it's rookie, but I just can't figure it out.
03-31-2009 04:00 PM
03-31-2009 04:02 PM - edited 03-31-2009 04:02 PM
Something like this?
03-31-2009 04:27 PM
03-31-2009 04:34 PM - edited 03-31-2009 04:34 PM
If what you have is an array of U8 values, and what you want is an array of U32 that packs four elements of your original array into a single element of the new array, all you need to do is typecast your array of U8 to an array of U32. Note that this doesn't do exactly what you specified in your original question, though, because you dropped leading 0s. If you do need those 0s removed then you'll need a more complicated solution, and a good explanation for why you'd want to do such a thing 😉

03-31-2009 05:05 PM
That doesn't seem to give me an array with anything in it.
I am talking to 4 spi chips. Each is the same except that it is configured with a different address. These addresses are incremental, 00, 01, 10, 00. So I can create an array by combining a loop and two constants. A command byte should combine these elements. 1000, address, 0. The command byte array should look like this when finished. 1000000, 1000010, 1000100, 1000110. It should be a binary number, as that is what the driver that talks to the spi channel utilizes.
I will take this command byte array, and interlace it with the command array. This will achieve an array with 8 elements. Alternating between command byte and command.
03-31-2009 07:40 PM - edited 03-31-2009 07:44 PM
OK, now I think I understand what you're trying to do. You need to take exactly 4 bits from the first array element, 2 bits from the second element, and 1 bit each from the third and fourth array elements, and combine them into a single U8 (based on the example in your initial post). You can do this with decimate array and some math, as shown below. Remember, though, that you need to set the radix of your array to binary in order for this to work, because there's a big difference between 10 decimal and 10 binary. If you're going to enter these in decimal you'll have to do a lot more work to combine them.
