For shifting out data to leds I need to convert individual settings stored in boolean/integer/char (1 = on, 0 = off)
from binary to decimal. For example:
Led1: on 1
Led2: on 1
Led3: off 0
Led4: on 1
settings: 1101
decimal value: 13
In processing, this can be done very easily with the unbinary() function.
But how would I do that in Arduino?
The "!= 0" part is probably not necessary but makes it clear what you're trying to accomplish. The "<< 3" part can be a variable instead of a constant (like "<< i").
I just found a good page on how to convert binary to decimal:
So I'll try to use this method and write my own code.
I think you did the same thing in your example, 16 being
2^4, but I'll have to code it myself to understand it .
I used 16 because that's the number of bits in an Arduino "int" (aka "short").
All my code snippet does is use the doubling method.
It starts from the most significant bit in the string representation of the number, checks to see if the string has terminated, else doubles the result so far, and then checks for an ASCII '1' (0x31), and if it's there, ORs in a 1 in the LSB position.
Because it doesn't check for the character '0' (0x30), this snippet would convert the string "1.1" to the number 5.