We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › convert a boolean Array to a byte Array
Page Index Toggle Pages: 1
convert a boolean Array to a byte Array (Read 1360 times)
convert a boolean Array to a byte Array
Oct 29th, 2008, 10:03pm
 
Hello,

i have a beginner problem converting an array of boolean values into a byte array.
I try to send the data of a two dimensional boolean Array [25][61] to Arduino. To have a quick data transfer i think it is the best to convert this boolean to byte. Now i tried a hole evening to manage this, and got already some grey hears. Can anybody help me?
Thanks.

Re: convert a boolean Array to a byte Array
Reply #1 - Oct 29th, 2008, 11:47pm
 
are you trying to just convert the 25x61 booleans into 25x61 bytes or are you trying to pack them into 25x61/8 bytes (ie 25x61 bits)?

boolean to byte is easy enough, just define an array the same size and

for (int x = 0 ; x < 25 ; x++) {
 for (int y = 0 ; y < 61 ; y++) {
   if (myBooleanArray[x][y]) {
     myByteArray[x][y] = 1;
   } else {
     myByteArray[x][y] = 0;
   }
 }
}

(i know nothing about arduino so this could all be wrong)
Re: convert a boolean Array to a byte Array
Reply #2 - Oct 30th, 2008, 8:48pm
 
Thank you for your reply.
I try to get 8 boolean into a byte. I found a way to do that but it is verry complicate. I am shure there is a easyer and better way to do that.


int xlinie = 61;
int ylinie = 25;
byte[][] ledarray= new byte[int((xlinie/8)+1)][25];
boolean[][] led= new boolean[int((xlinie/8)+1)*8][ylinie];
 for(int i=0;i<25;i++){
   for(int k=0;k<8;k++){
     String[] temp = {
       "0","0","0","0","0","0","0","0"          };
     String jointemp;
     for(int l=0;l<8;l++){
       if((led[(8*k)+l][i])==false){
         temp[l]="0";
       }
       else{
         temp[l]="1";
       }
     }
     jointemp=join(temp,"");
     ledarray[k][i]=byte(unbinary(jointemp));
   }
 }
Re: convert a boolean Array to a byte Array
Reply #3 - Oct 30th, 2008, 9:08pm
 
you have two nested loops of 1 to 8 there, are you sure that's what you want? (8 x 8 = 64 which is just bigger than 61?)

anyway, a faster way to convert 8 booleans to a byte, rather than using a string is,

byte b = 0;
for (int i = 0 ; i < 8 ; i++) {
 b = b << 1;
 if (bool[i]) {
   b |= 1;
 }
}

uses bitwise operators to shift and set bits:
http://processing.org/reference/bitwiseOR.html
http://processing.org/reference/leftshift.html
Page Index Toggle Pages: 1