I am compressing a byte array, and then converting each byte to something like this 000100111. When doing this, I get the following exception:
java.lang.NumberFormatException: For input string: "11111111111111111111111111011010"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:495)
at java.lang.Integer.valueOf(Integer.java:582)
at logic.ColorManager.getBinary(ColorManager.java:101)
at logic.ColorManager.convertByteToColor(ColorManager.java:47)
at logic.ColorManager.writeColors(ColorManager.java:86)
at logic.Main.main(Main.java:13)
The byte that causes the exception is: -32. This is my method to convert the bytes:
public String getBinary(byte byt) {
try {
int tmpNm = Integer.valueOf(Integer.toBinaryString((int) byt));
String tmpNmS = String.valueOf(tmpNm);
while (tmpNmS.length() < 8) {
tmpNmS = "0" + tmpNmS;
}
return tmpNmS;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
How do I convert a negative byte to its binary equivalent? eg. 01010010