After many calculations I have String containing binary representation of some data. How to convert it into array of ints in a consistent way.
I can take every 32 bits and put it into array but it doesn't seem like this solution.
Parsing each 32 bit section seems like a perfectly good solution to me. Note that it's probably best to use Long to do the parsing, to avoid problems if the leading digit is 1. For example:
public static int[] parseBinaryToIntArray(String input) {
// TODO: Validation
int[] output = new int[input.length() / 32];
for (int i = 0; i < output.length; i++) {
String section = input.substring(i * 32, (i + 1) * 32);
output[i] = (int) Long.parseLong(section, 2);
}
return output;
}
Corrected:
public static int[] convertIntoIntArray(String message) {
System.out.println(message.length());
int[] t = new int[message.length()/32];
for(int i = 0; i < t.length; i++){
//t[i] = Integer.parseInt(message.substring(i*32, (i+1)*32),2);
String section = message.substring(i*32, (i+1)*32);
t[i] = (int) Long.parseLong(section, 2);
// System.out.println(i+"fsa");
}
return t;
}