Pages: [1]   Go Down
Author Topic: equivalent to unbinary() in processing?  (Read 855 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 47
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 206
Posts: 12861
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
In processing, this can be done very easily with the unbinary() function
I thought unbinary converted ASCII strings representing binary numbers into numbers?

Logged

Per Arduino ad Astra

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 206
Posts: 12861
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

...into hexidecimal numbers.

- Brian
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 47
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley .

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

thank you!
yves
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
...into hexidecimal numbers

binary numbers

Later:
Code:
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;
}
« Last Edit: July 08, 2009, 07:22:50 am by GrooveFlotilla » Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 47
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just found a good page on how to convert binary to decimal:
http://www.wikihow.com/Convert-from-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 smiley-wink .

thank you anyway!
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
« Last Edit: July 08, 2009, 06:22:51 am by GrooveFlotilla » Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 47
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 47
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
 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
« Last Edit: July 11, 2009, 02:33:38 pm by yves » Logged

Pages: [1]   Go Up
Jump to: