convert string value into binary or decimal

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
kg300
Posts: 31
Joined: Thu May 31, 2018 6:25 am
Has thanked: 5 times
Been thanked: 1 time
Contact:

convert string value into binary or decimal

Post 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.?

mnf
Valued Contributor
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

Post 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 252 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

kg300
Posts: 31
Joined: Thu May 31, 2018 6:25 am
Has thanked: 5 times
Been thanked: 1 time
Contact:

Re: convert string value into binary or decimal

Post by kg300 »

Thanks 'mnf' its working as i required :D

Post Reply