Page 1 of 1

convert string value into binary or decimal

Posted: Mon Nov 05, 2018 10:33 am
by kg300
In a 485 serial network, one master and 4 slaves boards. Each Slave board read the position of 02 switchs attach with its port and send the switch logic state to master as a string.
For example if both 02 switchs are logic high it send string “11” and if both low it send string “00”.
master receive all 08 switches positions from slaves and adding them in a single string i.e “11110110” and this single string is the binary representation of all switches. Now my problem is how to convert this string value again to binary or decimal.?

Re: convert string value into binary or decimal

Posted: Mon Nov 05, 2018 7:31 pm
by mnf
The string you have is already binary - albeit represented as a string.

To convert it to a binary number (a byte, int etc) - you need to shift the digits into a variable:
bintodec.fcfx
(11.1 KiB) Downloaded 310 times
- for example converts a string (as per your example) to a variable - and the prints it to the UART here.
I've assumed the the string is in 'normal' binary (i.e. str[0] is the most significant bit)

Note that an alternate approach might be just to set the bits in the variable directly whilst testing the switches:
setbit.png
(20.99 KiB) Downloaded 1351 times
- you'd need to add some code to test the switches to this of course!



Martin

Re: convert string value into binary or decimal

Posted: Tue Nov 06, 2018 5:54 am
by kg300
Thanks 'mnf' its working as i required :D