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 & HelpPrograms › (newbie) get binary values
Page Index Toggle Pages: 1
(newbie) get binary values (Read 611 times)
(newbie) get binary values
May 13th, 2007, 10:30pm
 

hi there,

i'm a total newbie to proce55ing and struggling with my first sketch...

i want to import bytes from a data file and convert them to binary values (0,1).

the binary values should be stored in an array and be accessible seperately - i want to be able to access every single bit...

my first try:

byte b[] = loadBytes("data.dat");
 for (int i = 0; i < b.length; i++) {
   String a = binary(b[i] & 0xff);
   print(a);
 }

so i get a string of bytes in the text area. i dont know how to split it to single values and store them to an array...

can anybody help me, please...?

tia
peter
Re: (newbie) get binary values
Reply #1 - May 18th, 2007, 8:34am
 
peter,
I think this does it. The hack was subtracting 48 (ASCII value of '0') when parsing the bits.

Code:

byte[] b = {15, -75, 115, 127};
String a = "";

for (int i = 0; i < b.length; i++) {
a += binary(b[i]);
}

byte[] bits = new byte[a.length()];

for (int i = 0; i < bits.length; i++) {
// hack to get only nums 0, 1
bits[i] = byte(a.charAt(i)-48);
println("bits["+i+"] = " + (bits[i]));
}
Page Index Toggle Pages: 1