1. Home
    1. Dashboard
    2. Search
  2. Forum
    1. Unresolved Threads
    2. Members
      1. Recent Activities
      2. Users Online
      3. Team Members
      4. Search Members
      5. Trophys
  3. Articles
  4. Blog
  5. Videos
  6. Jobs
  7. Shop
    1. Orders
  • Login or register
  • Search
This Thread
  • Everywhere
  • This Thread
  • This Forum
  • Articles
  • Pages
  • Forum
  • Blog Articles
  • Products
  • More Options
  1. Robotforum - Support and discussion community for industrial robots and cobots
  2. Forum
  3. Industrial Robot Support and Discussion Center
  4. KUKA Robot Forum
Your browser does not support videos RoboDK Software for simulation and programming
Visit our Mainsponsor
IRBCAM
Robotics Channel
Robotics Training
Advertise in robotics
Sponsored Ads

convert an integer to binary KRC

  • ycrideas
  • January 5, 2023 at 4:45 PM
  • Thread is Unresolved
  • ycrideas
    Trophies
    1
    Posts
    12
    • January 5, 2023 at 4:45 PM
    • #1

    Hi guys!
    I need to convert an integer to binary, it would be something in the code itself, as if it had a DECL I=10 and the corresponding binary of I is returned. I didn't find the solution in the manual and I would like to see with you some possible solution. thanks

    Workvisual

  • Go to Best Answer
  • ycrideas
    Trophies
    1
    Posts
    12
    • January 5, 2023 at 5:01 PM
    • #2

    The Binary would be in the form of an array with 16 positions


    ex:

    Number: 3

    Array: F F F F F … F F T T

  • Online
    panic mode
    Reactions Received
    1,280
    Trophies
    11
    Posts
    13,080
    • January 5, 2023 at 5:33 PM
    • Best Answer
    • #3

    then just write your own... assuming this is 16-bit

    Code
    DEF DECODE16(val1:in, val2[]:out)
      DECL INT val1, i, n
      DECL BOOL val2[]
      i=1
      FOR n=1 to 16
         val2[n]=(val1 B_AND i)<>0
         i=i+i
      ENDFOR 
    END
    
    DEFFCT INT ENCODE16(val1[]:out)
      DECL BOOL val1[]
      DECL INT i, n, tmp
      tmp=0
      i=1
      FOR n=1 to 16
          IF Val1[n]==TRUE THEN
              tmp=tmp+i
          ENDIF
          i=i+i
      ENDFOR
      RETURN tmp
    ENDFCT
    Display More

    then you could use them to do something like

    Code
    DECL BOOL BitNumber[16]
    DECL INT MyValue1, MyValue2
    
    MyValue1 = 10
    DECODE16(MyValue1,BitNumber[])
    
    ; and the other way 
    BitNumber[1]=TRUE
    BitNumber[2]=FALSE
    BitNumber[5]=TRUE
    MyValue2 = ENCODE16(BitNumber[])
    Display More

    1) read pinned topic: READ FIRST...

    2) if you have an issue with robot, post question in the correct forum section... do NOT contact me directly

    3) read 1 and 2

  • ycrideas
    Trophies
    1
    Posts
    12
    • January 5, 2023 at 5:47 PM
    • #4

    thanks panic mode

  • ycrideas
    Trophies
    1
    Posts
    12
    • January 11, 2023 at 2:58 PM
    • #5

    morning, I managed to convert the integer, but the values that are filled in the array are the other way around, could you help me?

    in this case there will always be 16 array positions, but only the first ones will actually be used

    another thing man, I didn't quite understand what this ENCODE16 function does, can you explain? thank you so much

    Edited 2 times, last by ycrideas (January 11, 2023 at 3:32 PM).

  • Online
    panic mode
    Reactions Received
    1,280
    Trophies
    11
    Posts
    13,080
    • January 11, 2023 at 8:42 PM
    • #6
    Quote from ycrideas

    the values that are filled in the array are the other way around

    Just reverse index...

    N goes through values 1..16 so to get 16..1 replace N with 17-N:

    val2[17-n]=(val1 B_AND i)<>0

    ENCODE16 is inverse of DECODE16. It does exactly the opposite...

    1) read pinned topic: READ FIRST...

    2) if you have an issue with robot, post question in the correct forum section... do NOT contact me directly

    3) read 1 and 2

  • ycrideas
    Trophies
    1
    Posts
    12
    • January 13, 2023 at 12:25 PM
    • #7

    hi, I have a big problem,
    in this case it doesn't work as expected, in fact the array will be inverted, but it must be taken into account that several integers will not occupy the 16 positions of the array.

    for example the number 42 would be (0000000000010101) and in fact it should be (1010100000000000).

    when I use val2[n]=(val1 B_AND i)<>0 the expected array would be 1010100000000000 and it returns 0101010000000000, that is, the actually occupied positions are inverted

  • hermann
    Reactions Received
    407
    Trophies
    9
    Posts
    2,611
    • January 13, 2023 at 12:37 PM
    • #8

    42 binary is:

    0010 1010

    Your assumption 0001 0101 is wrong. You can see it on one sight. Lowest bit must be zero, as 42 is an even number.

  • Online
    panic mode
    Reactions Received
    1,280
    Trophies
    11
    Posts
    13,080
    • January 13, 2023 at 5:16 PM
    • #9
    Quote from ycrideas

    ...it returns 0101010000000000

    42 is 0000000000101010 but you wanted bit array reversed. so program returns correct output.

    1) read pinned topic: READ FIRST...

    2) if you have an issue with robot, post question in the correct forum section... do NOT contact me directly

    3) read 1 and 2

Advertising from our partners

IRBCAM
Robotics Channel
Robotics Training
Advertise in robotics
Advertise in Robotics
Advertise in Robotics

Job Postings

  • Anyware Robotics is hiring!

    yzhou377 February 23, 2025 at 4:54 AM
  • How to see your Job Posting (search or recruit) here in Robot-Forum.com

    Werner Hampel November 18, 2021 at 3:44 PM
Your browser does not support videos RoboDK Software for simulation and programming

Tag Cloud

  • abb
  • Backup
  • calibration
  • Communication
  • CRX
  • DCS
  • dx100
  • dx200
  • error
  • Ethernet
  • Ethernet IP
  • external axis
  • Fanuc
  • help
  • hmi
  • I/O
  • irc5
  • IRVIsion
  • karel
  • kawasaki
  • KRC2
  • KRC4
  • KRC 4
  • KRL
  • KUKA
  • motoman
  • Offset
  • PLC
  • PROFINET
  • Program
  • Programming
  • RAPID
  • robodk
  • roboguide
  • robot
  • robotstudio
  • RSI
  • safety
  • Siemens
  • simulation
  • SPEED
  • staubli
  • tcp
  • TCP/IP
  • teach pendant
  • vision
  • Welding
  • workvisual
  • yaskawa
  • YRC1000

Thread Tag Cloud

  • abb
  • Backup
  • calibration
  • Communication
  • CRX
  • DCS
  • dx100
  • dx200
  • error
  • Ethernet
  • Ethernet IP
  • external axis
  • Fanuc
  • help
  • hmi
  • I/O
  • irc5
  • IRVIsion
  • karel
  • kawasaki
  • KRC2
  • KRC4
  • KRC 4
  • KRL
  • KUKA
  • motoman
  • Offset
  • PLC
  • PROFINET
  • Program
  • Programming
  • RAPID
  • robodk
  • roboguide
  • robot
  • robotstudio
  • RSI
  • safety
  • Siemens
  • simulation
  • SPEED
  • staubli
  • tcp
  • TCP/IP
  • teach pendant
  • vision
  • Welding
  • workvisual
  • yaskawa
  • YRC1000

Similar Threads

  • How to receive integers frpm PLC to a KR10 R1420 KRC4 V8.3

    • Doe
    • June 27, 2019 at 3:02 PM
    • KUKA Robot Forum

Tags

  • convert int to binary
  1. Privacy Policy
  2. Legal Notice
Powered by WoltLab Suite™
As a registered Member:
* You will see no Google advertising
* You can translate posts into your local language
* You can ask questions or help the community with your knowledge
* You can thank the authors for their help
* You can receive notifications of replies or new topics on request
* We do not sell your data - we promise

JOIN OUR GREAT ROBOTICS COMMUNITY.
Don’t have an account yet? Register yourself now and be a part of our community!
Register Yourself Lost Password
Robotforum - Support and discussion community for industrial robots and cobots in the WSC-Connect App on Google Play
Robotforum - Support and discussion community for industrial robots and cobots in the WSC-Connect App on the App Store
Download