Convert a REAL into 2 consecutive Ints

rjgower

Member
Join Date
Feb 2017
Location
Michigan
Posts
6
I converted 2 consecutive Ints to a Real using a Copy instruction. So now I need to reverse this process. Convert a Real into 2 consecutive Int's. Is this possible and if so how?
 
You could use a COP instruction to do this step as well. Your length would be 2 instead of 1 since length is based on destination element size.
 
I was able to make that work with converting 2 Int's to a Dint, but I was unable to do so the opposite way.

You may need to swap word or byte data using the SWPB instruction since you're coming from a REAL. I've made it work before but don't have the logic in front of me presently to tell you how.
 
Going from 32 bits to 2 ints you loose 1 bit on the way because two ints has two bits total for sign. You can do it with unsigned int I guess. And also, why real and not dint? A real can be much larger than a dint even though they are both 32 bits.

Are you using signed or unsigned ints? What's the max of your real? Is the real always coming from a combination of two ints (it will always divide into two ints with no decimals)?
 
Last edited:
if you go from two uint (0-65535) to a real and then back, if the int's are on the larger side of the scale you'll loose information because of how a real works. See these two excellent posts on the subject:

http://www.plctalk.net/qanda/showpost.php?p=489034&postcount=2

http://www.plctalk.net/qanda/showpost.php?p=489035&postcount=3

from this thread:
http://www.plctalk.net/qanda/showthread.php?t=72290


If you go from signed integers to a dint (using 0-32767 as the range, ignoring negative numbers) and then back it will work and give the same numbers on both sides no matter what you put into it. Two uints (16bits x2) won't fit in a dint (31bits + sign).

int_to_dint_and_back.jpg
 
Last edited:
I assume you are using 2 INTs to transfer the REAL to another processor ?


If so, COP will work for both conversions, since COP does absolutely nothing to the bit patterns, it will re-create the REAL


One picture shows REAL to INTs to REAL, the other shows INTs to REAL to INTs. In either case you should ignore the actual values of the "middle" tag, as you can see the bit patterns of the 2 INTs when copied into a REAL do not produce a valid REAL number, so you get the error -1.#QNAN, which means "not a number", but the end-to-end data is correct.

2018-07-17_124207.jpg 2018-07-17_124735.jpg
 
Last edited:

Similar Topics

Hi all. Me again still learning Rockwell. So I'll be polling an INT data array from a Modbus SE power meter with a L82 (with a Modbus ProSoft in...
Replies
56
Views
1,208
Hello, First, please excuse my question I am new to Modbus. I have a CX-5140 control unit from Beckhoff and an instrumentation device that...
Replies
3
Views
3,017
Greetings fellow PLC programmers, I know there is a simple way to get the system date time of the RSLogix system. But, is there then a way to...
Replies
12
Views
8,375
Hi Guys, i'm begginer and i'm trying to convert a real value (not fix value) to BCD. It works if i put fix real value but when i put variable...
Replies
2
Views
1,912
From a Modbus read I am getting back and array of words. Within that array, 2 of the words represent a real value. Nominally all I have to do is...
Replies
1
Views
1,360
Back
Top Bottom