LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

I need to reverse a byte

I am trying to send data over TCP/IP into a Labview VI. My problem is that I accidentally reversed the order of bits in my hardware. So I thought it would be alot easier to fix this in software, rather than hardware. So, all I need to do is reverse the order of bits in a byte, i.e. make 01234567 into 76543210. Is there an easy function to do this, or do I have to make one myself? Thanks.
0 Kudos
Message 1 of 12
(5,003 Views)
hi,
i the easilest way would be to convert the byte array in to string then use the reverse string function and reconvert it to byte and send it across.These are all built in functions in labview.so your job will be made easy
hope this solves your problem
vicky
0 Kudos
Message 2 of 12
(5,003 Views)
There are also bit and byte functions on the Advanced>Data Manipulation palette.
0 Kudos
Message 3 of 12
(5,003 Views)
I already tried the bit and byte functions. None of them do what i want directly. Those functions only shift bits left and right.
0 Kudos
Message 4 of 12
(5,003 Views)
This was a good idea, but unfortunately it doesn't work for me. Here's an example: I am trying to turn 01000000 into 00000010. When I turn 01000000 into a string the result is the character @. When I reverse @, I get @. @ converted back to string is 01000000. Any additional help is greatly appreciated.
0 Kudos
Message 5 of 12
(5,003 Views)
JoeLaRosa;

In the boolean palette there is a function that converts a number into a boolean array, and its counterpart which converts a boolean array into a number.

Convert the number into a boolean array, reverse the array (there is a function in the array palette that does that) and then convert the boolean array back into a number. It is possible that you may need to recast the number to its original type.

Regards;
Enrique
www.vartortech.com
Message 6 of 12
(5,003 Views)
Enrique's method should accomplish exactly what you need.

Attached is how the code should look.

Good luck with development.

Regards,

Matt F
0 Kudos
Message 7 of 12
(5,003 Views)
Enrique, this is exactly what I did to fix the problem. The only issue with this solution is that you must have a single numeric going into the convert to boolean array function. I had an array of bytes. All I had to do was index the array and send that value into the function. Now it works great!!! Just a few labview functions saved me hours in re-wiring my hardware.
0 Kudos
Message 8 of 12
(5,003 Views)
I have an alternative solution here that I thought might be more efficient because it uses the bit-shifting functions rather than building and reversing an array. What it does is to successively rotate-right the original byte, passing the carry into a rotate left which builds the reversed byte.

However, an actual time-trial test comparing this method with Enrique's showed only a very modest speedup. Also, Enrique's solution works seamlessly for 8, 16, and 32 bit integer types whereas mine is hardcoded for 8 bits only. So I'd have to recommend his method over mine.

Nevertheless, attached is a picture of my code for the still-curious:

-Kevin P.
ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 9 of 12
(5,003 Views)
JoeLaRosa wrote in news:50650000000500000034D90100-
1079395200000@exchange.ni.com:

> I already tried the bit and byte functions. None of them do what i
> want directly. Those functions only shift bits left and right.

I just made a quick test.

You can use the "Rotate Left With Carry" and "Rotate Right With Carry" to
do this. You need a for loop with two shift registers to do all 8, 16 or 32
bits in your number.

The other possibility, to use "Number to Boolean Array", "Reverse 1D Array"
and "Boolean Array to Number" is quicker.

For both 16 and 32 bits number i get that using the bit shift functions
take approximately 2.5 times longer than using the "number to boolean
array" route.

Pentium 450, Windows NT, LabVIEW 6.0.2,


--
Rolf
0 Kudos
Message 10 of 12
(5,003 Views)