コードに小さなエラーがあり、一生理解できません。
たとえば、バイナリデータ(16進数から変換した後)を表す文字列の配列があります。1つのインデックスは1011で、もう1つは11100です。配列を調べて、各インデックスに0を埋め込み、各インデックスが8バイトになるようにします。これらの表現を実際のバイトに変換しようとすると、「11111111」を解析しようとするとエラーが発生します。発生するエラーは次のとおりです。
java.lang.NumberFormatException: Value out of range. Value:"11111111" Radix:2
スニペットは次のとおりです。
String source = a.get("image block");
int val;
byte imageData[] = new byte[source.length()/2];
try {
f.createNewFile();
FileOutputStream output = new FileOutputStream(f);
for (int i=0; i<source.length(); i+=2) {
val = Integer.parseInt(source.substring(i, i+2), 16);
String temp = Integer.toBinaryString(val);
while (temp.length() != 8) {
temp = "0" + temp;
}
imageData[i/2] = Byte.parseByte(temp, 2);
}