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.?
convert string value into binary or decimal
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: convert string value into binary or decimal
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:
- 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:
- you'd need to add some code to test the switches to this of course!
Martin
To convert it to a binary number (a byte, int etc) - you need to shift the digits into a variable:
- 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:
- you'd need to add some code to test the switches to this of course!
Martin