equivalent to unbinary() in processing?

hi there

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?

Thanks for any help!
yves

byte LedValues;
boolean Led1;
boolean Led2;
boolean Led3;
boolean Led4;

LedValues = 13;

Led1 = ((LedValues & (1 << 3)) != 0);
Led2 = ((LedValues & (1 << 2)) != 0);
Led3 = ((LedValues & (1 << 1)) != 0);
Led4 = ((LedValues & (1 << 0)) != 0);

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").

  • Brian

In processing, this can be done very easily with the unbinary() function

I thought unbinary converted ASCII strings representing binary numbers into numbers?

...into hexidecimal numbers.

  • Brian

yes, unbinary in my exmaple would work like this:
unbinary("1101") would result in 13.

brian, I dont quite understand your code. I'm trying to get to
the value 13, not the other way around :slight_smile: .

but still, can you explain what you are doing? it seems to be
some kind of abreviated java script?

thank you!
yves

int val = 0;
for (int i = 0; i < 16; i++) {
  if (! string [i]) {
    break;
  }
  val <<= 1;
  val |= (string [i] == '1') ? 1 : 0;
}

...into hexidecimal numbers

binary numbers

Later:

int unbinary (char* string)
{
  int val = 0;
  for (int i = 0; i < 16 && string [i]; i++) {
    val <<= 1;
    val |= (string [i] == '1') ? 1 : 0;
  }
  return val;
}

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 :wink: .

thank you anyway!

16 being 2^4

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.

thank you for the explanation! I'll try to code the same
with the table method, just to get a better understanding
of the matter.

best
yves

ok, I now have the equivalent to using the table method.
this code converts a binary string (in this case 16 figures long)
to its decimal equivalent:

  for (i = 0; i < 16; i++){
    settingsValue = settingsValue + settings[15-i]*int(pow(2,i));
  }

I think such a method should be added to arduino...

best
yves
edit: settings(i) is the array that contains the binary values as integer