I am writing a method where I am converting int values into binary strings and storing them. I am using the method Integer.toBinaryString to do so, and it's working correctly, but the problem is that I need the method to return exactly 4 bits in the string instead of less (it will never be more because the numbers are not large enough). Here is an example of my code and where the problem is occurring:
int value5 = 3;
String strValue5 = Integer.toBinaryString(value5);
for(int index = 0; index < 4; index++){
sBoxPostPass[4][index] = strVal5.charAt(index);
}
Clearly, this will throw an ArrayOutOfBoundsException because strValue5 == 11
and not 0011
, like it needs to be. I hope this is sufficiently clear. Thanks in advance for the help.