Integer type to byte array

esse
2015-11-24
2015-11-25
  • esse - 2015-11-24

    Hello, another question from a Codesys newbie, is a possible way to convert an integer value to a byte array,for example i try to make a program that the user will insert a value such 567 and i want to convert it to 4 bytes
    Thank you in advance

     
  • ndzied1 - 2015-11-24

    An INT is only 2 Bytes Long. There are several ways to achieve this.

    One is to create a union with an INT and an array of BYTES

    TYPE TwoByte : UNION
    Β  Β i: INT;
    Β  Β bytes: ARRAY [0..2] OF BYTE;
    Β  Β 
    END_UNION
    END_TYPE
    

    If you create a variable:

    MyVar: TwoByte;
    

    Then you can copy the integer into

    MyVar.i
    

    Then the two bytes that make up that integer will be available at:

    MyVar.bytes[0]
    MyVar.bytes[1]
    
     

    Related

    Talk.ru: 1

  • esse - 2015-11-25

    Thank you very much for your reply, if I write this code I receive an error about UNION, is it a codesys code?

     
  • learnetk - 2015-11-25

    Actually in older Codesys you Need to activate the Proffesinal Feature to find Union.

    IMG: Union.png

    IMG: feature.png

     

Log in to post a comment.