LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert a Single Number to a 32-bit hex string and swap the high and low bytes

Solved!
Go to solution

I need to convert a Single Precision Floating Decimal number to 32-bit hexadecimal form, swap the high and low bytes, and convert that to a hex string.  I have tried using the "Swap Word" function but it doesn't seem to have any affect on the Single number.  

 

So

 

Input a Single Decimal # : 100

Convert to 32 bit Hexadecimal String with the low byte first: 0000 42C8

 

 Any one have any ideas?

 

Thanks

0 Kudos
Message 1 of 11
(10,014 Views)

Use "flatten to string" and set the byte order to little endian.

0 Kudos
Message 2 of 11
(10,011 Views)

awesome! thanks

0 Kudos
Message 3 of 11
(10,001 Views)

Actually, the above answer is incorrect if you really want the 100 -> 0000 42C8 functionality. You seem to want an unusual conversion.

 

Try this instead:

 

(left to right: typecast to U16 array, reverse array, typecast to string)

 

0 Kudos
Message 4 of 11
(9,998 Views)

Sorry spoke too soon...

 

That using flatten string with little endian gives me "0000 C842" instead of "0000 42C8"

0 Kudos
Message 5 of 11
(9,997 Views)

Yeah that's what i'm looking for.  I am using Modbus RTU to control a motor controller and it calls for its data in that format.

0 Kudos
Message 6 of 11
(9,982 Views)
Solution
Accepted by topic author drb12345

Slighly more lightweight, try casting to U32 and swap the words. Same difference. 😉

 

(apparently, swap words is a NOOP for SGL, but still accepts SGL without coercion dot. Interesting...)

 

Message 7 of 11
(9,979 Views)
You should unmark message 2 and instead mark message 4 or 7 as solution. Thanks!
0 Kudos
Message 8 of 11
(9,969 Views)

 


@altenbach wrote:

(apparently, swap words is a NOOP for SGL, but still accepts SGL without coercion dot. Interesting...)


I have the feeling that the current behavior of swap words is incorrect for SGL inputs. I created an note in the monthly bug thread. Any comments are welcome.

 

0 Kudos
Message 9 of 11
(9,945 Views)

 


@altenbach wrote:

 


@altenbach wrote:

(apparently, swap words is a NOOP for SGL, but still accepts SGL without coercion dot. Interesting...)


I have the feeling that the current behavior of swap words is incorrect for SGL inputs. I created an note in the monthly bug thread. Any comments are welcome.

 


 

My guess is that this is somewhat intended behavior.  You are able to wire a cluster with multiple data types and it will swap bytes/words on integers and leave the others alone.  Not that I have ever thought about doing this....

 

ByteSwapCluster.png

 

still seems a bit strange.

 

Detailed Help: data is an integer, an array of integers, or a cluster containing integers that you want to byte swap. If data is a cluster that contains integers, this function swaps only the integer elements of the cluster.

 

 

0 Kudos
Message 10 of 11
(9,935 Views)