我的代码中有一个小错误,我一生都无法弄清楚。
我有一个字符串数组,它们表示二进制数据(从十六进制转换后),例如:一个索引是 1011,另一个是 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);
}