日期:2014-05-20  浏览次数:20843 次

一个文件里是16进制拿出来指定的数也可以说是字符错哪里了
import java.io.FileInputStream;
import java.io.IOException;


public class getthatbyte {
static byte[]b = new byte[1024];
public static byte[] rFile(byte[]b){
try {
FileInputStream input = new FileInputStream("c:test.fsv");
input.read(b, 4, 4);
} catch (IOException e) {
e.printStackTrace();
}
return b;
}

public static  void main(String[] args)
{
getthatbyte.rFile(b);
String hex = new String();
for(int i=0;i<=b.length;i++)
{
hex = Integer.toHexString(b[i]&0XFF);
if(hex.length()==1)
{
hex = hex+'0';
}
}
System.out.println(hex.toUpperCase());
}
}

------解决方案--------------------
数组下标越界了吧?
------解决方案--------------------
引用:
00
00
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1024
at getthatbyte.printhexstring(getthatbyte.java:22)
at getthatbyte.main(getthatbyte.java:34)
改了下 我只要四个字节 打印出来这样的 再想想 


你这不就是数组下标越界嘛,你的i<=b.length应该改成i<b.length