Convert array of 4 byte to real

nejimbarek
2021-06-29
2021-06-30
  • nejimbarek - 2021-06-29

    hello,
    i'm new in coding with codesys
    is there any help about how to convert array of 4 byte to real
    thank you for your support

     
  • Ingo

    Ingo - 2021-06-30

    It depends. First you need to know more about the exact format.

    Is it a 32bit IEEE floating point number, which is just packed into an array of 4 bytes?

    Then, s.th. like this:

    byValue : ARRAY OF BYTE;
    prValue : POINTER TO REAL;
    rValue : REAL;
    
    prValue := ADR(byValue);
    rValue := prValue^;
    

    Actually you use pointers to the data to cast it to the correct data type.

     
  • i-campbell

    i-campbell - 2021-06-30

    I ran into troubles with that method on ARM processors which only allow reals when they are alligned to a memory address divisible by 4. the rValue := prValue^; causes an access violation.

    Instead use either a memCpy or a byteBuffer. with bytebuffer you even have the option to do byteswapping.

     

Log in to post a comment.