After trying every answer on this topic I could find (and searching for almost a full day), I've been unable to successfully convert binary data in string format (variable a) to an alphanumeric string. My question is "What am I doing wrong?"
Ultimately I want this to print out a human readable string, such as "Hello There", which would be the integer 87521618088882520788922981 (input will be changed to long.)
int input = 256;
int maxInt = 10000;
String result = "";
String o;
String a = "";
String stringByte;
char c;
int pos;
while (input <= maxInt){
System.out.println("Input: " + input);
o = Integer.toBinaryString(input);
System.out.println("Binary String: " + o);
System.out.println("Remainder: " + o.length() % 8);
if (((o.length() % 8) == 0) && (o != "0")){
//is a multiple
a = o;
}else{
a = o;
while ((a.length() % 8 != 0)){
a = "0" + a;
}
}
//divide into bytes
System.out.println("Zero-putter: " + a);
int numBytes = a.length()/8;
pos = 1;
System.out.println("Bytes Received: " + numBytes);
byte[] bytes = a.getBytes();
String str = new String(bytes);
System.out.println("Character: " + str);
System.out.println("-----------------------------");
input++;
try {
Thread.sleep(1000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
Answers I've tried:
- Binary to text in Java
- Convert binary string to ascii text?
- Parsing a string of binary into text/characters
- How to convert efficiently binary string to binary byte array in Java?
- http://www.dreamincode.net/forums/topic/247956-convert-binary-data-to-text/
- How do I convert a large binary String to byte array java?
- How do I convert a large binary String to byte array java?