Convert INT to Binairy

henk73
2022-08-29
2022-08-31
  • henk73 - 2022-08-29

    HAllo,
    I am new with codesys.
    I want to convert a INT to a 4 bit binairy number
    I have 4 inputs
    I connect all the input to a AND Block and the result must be an binairy number

    I want a VAR output like for example 0010 or 0001

    Thanks

     
  • fajean - 2022-08-29

    When you say "to a 4 bit binary number", what is it that you mean by "binary number"? A four-character string with each character being either 0 or 1? A numeric variable (of 8, 16 , 32 or 64 bits) with only the four less significant bits assigned (which can code numbers from 0 to 15)?

     
    • henk73 - 2022-08-29

      Hello

      Yes only 0 & 1.
      And when it is possible 4digits
      0001 or 0101.
      When it is 8digits it is also ok

      Outlook voor Androidhttps://aka.ms/AAb9ysg downloaden


      From: talk@forge.forge.codesys.com talk@forge.forge.codesys.com on behalf of fajean no-reply@codesys.com
      Sent: Monday, August 29, 2022 5:02:16 PM
      To: [forge:talk] Engineering@talk.forge.forge.codesys.com
      Subject: [forge:talk] Convert INT to Binairy

      When you say "to a 4 bit binary number", what is it that you mean by "binary number"? A four-character string with each character being either 0 or 1? A numeric variable (of 8, 16 , 32 or 64 bits) with only the four less significant bits assigned (which can code numbers from 0 to 15)?


      Convert INT to Binairyhttps://forge.codesys.com/forge/talk/Engineering/thread/611c1dc551/?limit=25#35ec


      Sent from forge.codesys.com because you indicated interest in https://forge.codesys.com/forge/talk/Engineering/

      To unsubscribe from further messages, please visit https://forge.codesys.com/auth/subscriptions/

       
      • fajean - 2022-08-29

        Any type is binary, in the sense that all values are represented in memory as sequences of 0 and 1. Your INT is already made up of 0 and 1, which you can directly access.

        For instance, if you have a variable like this :

        myint: INT;
        

        You can access each bit like this :

        least_significant_bit := myint.0;
        most_significant_bit := myint.15;
        

        You could also use bitwise operators (SHL, SHR, ROL, ROR) to perform bitwise manipulation of values.

        Getting specific bits out of a numerical value in CODESYS is therefore easy.

         

        Last edit: fajean 2022-08-29
  • henk73 - 2022-08-30

    Hello Fajean,

    Thanks for your explanation, but I don't quite get it yet. SORRY!!!!
    In November i get training :)
    Would you mind making an example for me in Codesys?
    My example is as follows:
    I have 4 inputs (sensors)
    If sensor 1 high, output 1000
    Sensor 2 high, then make output 0100
    Sensor 3 high, then make output 0010
    Sensor 5 high, then make output 0001

    thanks in advance
    gr Henk

     
  • henk73 - 2022-08-30

    Hello Fajean,

    Thanks for your explanation, but I don't quite get it yet. SORRY!!!!
    In November i get training :)
    Would you mind making an example for me in Codesys?
    My example is as follows:
    I have 4 inputs (sensors)
    If sensor 1 high, output 1000
    Sensor 2 high, then make output 0100
    Sensor 3 high, then make output 0010
    Sensor 5 high, then make output 0001

    thanks in advance
    gr Henk

     

Log in to post a comment.