Okay, A friend helped me with this code a little bit. I understand what everything else does and why it does it except for one thing. Where is this 128 coming from?? Also, this program runs, pulls the string from the file, converts it to binary, but takes all of the spaces out, so that when you re convert the binary back to the string, it is all one word. So what is the 128 and what can I do to keep the spaces?
/******************************* *I fixed it, Thanks for your help guys! * *I have changed the code so you can see how I fixed it. * *******************************/
public static void main(String[] args) {
String text = "My string to binary works too";
byte[] bytes = text.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes){
int val = b;
for (int i = 0; i < 8; i++){
binary.insert(0, (val & 1) == 0 ? 0 : 1);
val >>>= 1;
}
binary.insert(0, ' ');
System.out.print(binary);
}
}
}