小编Jos*_*osh的帖子

Java Byte.parseByte()错误

我的代码中有一个小错误,我不能为我的生活弄清楚.

我有一个字符串数组,它们是二进制数据的表示形式(在从十六进制转换之后),例如:一个索引是1011,另一个是11100.我遍历数组并用0填充每个索引,以便每个索引是8个字节.当我尝试将这些表示转换为实际字节时,当我尝试解析'11111111'时出现错误我得到的错误是:

java.lang.NumberFormatException: Value out of range. Value:"11111111" Radix:2
Run Code Online (Sandbox Code Playgroud)

这是一个片段:

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);
}
Run Code Online (Sandbox Code Playgroud)

java parsing byte numberformatexception

7
推荐指数
1
解决办法
2万
查看次数

标签 统计

byte ×1

java ×1

numberformatexception ×1

parsing ×1