S16 To U32


Recommended Posts

What's the cleanest (fastest?) way to convert two values which DAQ Factory has polled as S16's but which are actually halves of a U32, back into that U32? The values are coming in as S16's to avoid breaking a consecutive string of registers.

Convert the S16's to bits or bytes, append, and back to a U32?

An iif that converts the S16 to a U16, then combine using (U16#1 << 16 + U16#2)?

Link to comment
Share on other sites

LSW + MSW * 65536

There are other ways, but that's probably fastest. If you were doing low level assembly programming, then << is marginally faster, but then if you were doing that, you would just move the two memory locations so they were neighbors and cast it to a U32.

You probably really should poll as U16, not S16.

Link to comment
Share on other sites

Yes, if you have to use S16, then simple math won't work. There are a couple options:

to.uLong(concat(transpose(from.Word(U16#1),0), transpose(from.Word(U16#2),0)))

Not sure of the byte ordering...

Or, you can convert an S16 to U16 by doing:



to.uWord(from.word(LSW)) + to.uWord(from.word(MSW)) * 65536

Or to make it easier, just create a Conversion:


and apply that to any S16 values that you really wanted to be U16. Remember, the S16 vs U16 only applies in the Modbus portion. Once its in the channel and ready to be converted, its just a double float.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.