LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Converting 3 bytes to a I32

Solved!
Go to solution

I have 3 U8 bytes in an array (a byte received 24bit number). How can I convert them into a 2's compliment I32 number for general use?

0 Kudos
Message 1 of 13
(6,874 Views)

First of all, there are 4 bytes in an I32.  So what do we do with the extra byte?  Where does this extra byte go?

 

Look into the Join Numbers in the Numeric->Data Manipulation palette.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 2 of 13
(6,861 Views)

Still a bit lost. I am planning on using the "Type Cast" function to change a byte (U8) array of length 3 into an I32. It has the useful feature that it places the bytes from the array in the upper part of the created I32. It means, that if I had an Arithmetic Shift operation, I could propogate the MSBit (bit 31) down to bit 23, thereby preserving the 2's compliment nature of the value. So a negative 24bit number is the same as in a 32bit number.

 

My problem is, I am not sure whether there is a better Shift function than the "Shift Right With Carry" function?! If I use that, it means I need to expand the number to a Boolean array, just to get at Bit31 to set as the CarryIn of the "Shift-Right With Carry" function. It seems over the top to me.

 

Anything better available?

 

Thanks.

0 Kudos
Message 3 of 13
(6,854 Views)

What's wrong with the Logical Shift function?


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 4 of 13
(6,845 Views)

It does not preserve the state of the MSBit. Thereby losing the 2's compliment nature of the number when you shift it down to its proper position in the 32bit value.

Its the 2's compliment aspect of this which I am wrestling with. In C text-based programs its easy. I am looking for the equivalent in LabVIEW.

0 Kudos
Message 5 of 13
(6,843 Views)

How would you do it in C?

 

Here's what I'm guessing you are looking for.  Still not entirely sure if I got it yet.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 6 of 13
(6,825 Views)

You can also do it using the Select function, if you prefer less logic functions. Same skin off the cat.

 

 

As you can see, LabVIEW is just as capable as C at bit-twiddling.

0 Kudos
Message 7 of 13
(6,818 Views)

For what its worth, I got this to work. But like you, would like to avoid arrays. Is there no simple way of getting a single bit (boolean) from a numeric?

 

2sCompConv.PNG

0 Kudos
Message 8 of 13
(6,811 Views)

Wow, that's .... excessive. You should look at the two examples posted. LabVIEW is no different than C in terms of numerics. In C, you don't have bits, but bytes. If you want to know if a certain bit is 1 or 0 you AND the numeric with an appropriate mask, and check if the resultant numeric is 1 or 0. Same thing in LabVIEW.

0 Kudos
Message 9 of 13
(6,808 Views)
Solution
Accepted by topic author skol45uk

I wanted to make a .vi so I could get a 2's complement value from the contents at any position in a U8 array. My problem being that I have a byte stream which is feeding encoded values that can be 32 bits, 24 bits or 16 bits in size. I would like to be able to somehow take the 'type' parameter out to make it really general purpose. Do you know of any vi's that already do all this?

 

I may even have to deal with odd size values like 10bits!

 

Thanks very much for the simplified solution. I may just go with simpler bespoke code for each extraction type I need to make.

 

I am beginning to see the parallels with C. Though I still think an Arithmetic Shift would be useful. Especially as the Type-Cast operator conveniently positions the value it reads from its input, at the top end of the 32bit value.

 

Thanks again.

 

 

 

 

 

 

0 Kudos
Message 10 of 13
(6,802 Views)