Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Convert 2 INT to Real

lior
2021-08-15
2021-08-15
  • lior - 2021-08-15

    Hi,
    I want to convert 2 diff INT datatype TO 1 Real datatype.
    for example:
    int1: int
    int2:int
    real3:real
    How Can convert int1&int2 TO Real3?
    Thanks

     
  • hermsen

    hermsen - 2021-08-15

    In order to give you a good anwser, you need to be more specific as your question can be interpreted in twofold. I interpret you question with a simple anwser on face value;

    Do you want INT A + INT B = REAL C?

    rC := INT_TO_REAL( iA + iB);
    

    If you want something more complex, please explain very specific what you want to accomplish.

    Regards

     
    • lior - 2021-08-15

      I have 2 different variable data type INT
      I want to convert: 2 Bytes from the first var =INTA and 2 Bytes from the second var=INTB INTO REALC

       
  • lior - 2021-08-15

    Can I use it like:
    TYPE Convert_2_int_to_byte :UNION

     i: WORD;
     c:WORD;
    

    bytes: ARRAY [0..3] OF BYTE;

    END_UNION
    END_TYPE

    TYPE Convert_Real_to_byte :UNION

    a:REAL;
    bytes: ARRAY [0..3] OF BYTE;

    END_UNION
    END_TYPE

     
  • hermsen

    hermsen - 2021-08-15

    nope...

    Google for IEEE 754
    https://en.wikipedia.org/wiki/IEEE_754

    unless...

    Your INT's (Bytes) allready have been 'marshalled' to hold IEE 754 bit and bitorder and you want to glue the bytes together again to get the original REAL (Float)

    Are you using Profibus or MODBUS so?

     

    Last edit: hermsen 2021-08-15
    • lior - 2021-08-15

      I am using MDLC.
      I want to glue the bytes together INTA , INTB to get REALC(Float)

       
  • hermsen

    hermsen - 2021-08-15

    MDLC -> Google brings up nothing, what is a MDLC?

    glue the bytes together INTA , INTB to get REALC(Float) -> Google for IEEE 754 to understand a float. https://en.wikipedia.org/wiki/IEEE_754

    If you want no marshalling you can use the union solution you posted before

     

    Last edit: hermsen 2021-08-15
  • ojz0r - 2021-08-15

    If you define a new variable as Dint for example you can use:

    diNewVar := SHL(int1, 16) + int2;
    real3 := dint_to_real(diMyVar);
    

    Ofcourse real3 will still be an integer value in that case.

    Edit: Im not in front of an IDE right now so i assumed there is an Dint_to_real function, otherwise use a format that is convertable for the "diNewVar".

     

    Last edit: ojz0r 2021-08-15

Log in to post a comment.