convert decimal to binary and contrary ! - Printable Version +- Logic Machine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8) +--- Thread: convert decimal to binary and contrary ! (/showthread.php?tid=3948) |
convert decimal to binary and contrary ! - phongvucba - 23.03.2022 Hi guys ! I have a little problem, I have 3 problems, please help! : - I want to convert Decimal value to binary, or Hex to binary. Example: Number 5000 converts to 0001 0011 1000 1000, suppose assigning to string b='0001 0011 1000 1000' - After getting string b, we can read the value of each bit in string b (read each position from bit 0 to bit 15). For example, for the string b='0001 0011 1000 1000', I read bits from right to left, the 4th place is "1", the 10th and 11th places are "1" and "0". for the string b='0001 0011 1000 1000', I also want to read the ordinal bits, the 9th and 10th and 11th positions are "111", the 3rd and 4th positions are "10" How can I do this? This is "how to convert dec to bin" https://www.rapidtables.org/vi/convert/number/decimal-to-binary.html - I have the string b='0001 0011 1000 1000'. I want to record the 4th value =1 (from right to left) as =0 . What should I do .After assigning I want to convert this string to decimal number Thank you very much everyone! RE: convert decimal to binary and contrary ! - admin - 23.03.2022 Use this function, it returns a number that is stored in bits starring from offset to offset + length (not inclusive). offset is the lowest bit number that you want to get. length is number of bits to get. Code: function getbits(value, offset, length) Note that bits are counted from right to left starting from 0. So bits 9..11 are this: 0001 0011 1000 1000 RE: convert decimal to binary and contrary ! - phongvucba - 23.03.2022 Specifically I am wanting to control the air conditioner via modbus rtu. But my device only sends back the value of the register as Decimal for LM. But the modbus documentation of the conditioner must read and send commands via binary value. So I have to convert from Decimal to Binary. But the problem now how do I send the command into any of the 0-15 bits? Through Modbus poll software, I can do this, but on LM I can't do it I am very happy for your help! But how do I convert the value 5000 to a binary value, and vice versa I have 1 binary value and then convert it to a decimal value? RE: convert decimal to binary and contrary ! - Daniel - 23.03.2022 You can write bits to modbus, there are many examples see this https://forum.logicmachine.net/showthread.php?tid=3941&pid=25448#pid25448 RE: convert decimal to binary and contrary ! - phongvucba - 24.03.2022 oh..thank so much Daniel ! I will check. I read through this, but don't quite understand, sorry I'm new to modbus. Hope everybody help please! I have the following example, the value of the register is as follows (42014): To select the dry mode is: 0100 0010 0000 0111 16903 dry mode To select the cool mode is: 0100 0010 0000 0010 16898 cool mode To select fan mode is: 0100 0010 0000 0000 16896 fan mode Thus, when selecting modes, we only need to send the change command for 3 bits 0,1,2 from left to right (here, 111, 011, and 010). How can I send these bits? . Please help me. Thank so much ! RE: convert decimal to binary and contrary ! - admin - 24.03.2022 This can and should be done via a profile. Mapping entry should look like this. I'm not sure about the address 42014, it might be 2014 or 2013. Code: { Later you can assign custom values to the mapped object to convert numerical values to text. You can use Windows calculator in programmer mode to get bitmasks for certain bits. Switch to bit input mode, then select the required bits (0..2 in this example). Use the decimal value ask value_bitmask (7 in this example). |