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());
}
}

解决方案 »

  1.   

    噢……下标好像没关系
    不过我不懂这个for循环在做什么?for(int i=0;i<=b.length;i++)
    {
    hex = Integer.toHexString(b[i]&0XFF);
    if(hex.length()==1)
    {
    hex = hex+'0';
    }
    }
      

  2.   

    你的每次循环都重置了hex的值,应该用一个字符串把每次转换出来的字符存起来,最后一起print
      

  3.   

    00
    00
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1024
    at getthatbyte.printhexstring(getthatbyte.java:22)
    at getthatbyte.main(getthatbyte.java:34)
    改了下 我只要四个字节 打印出来这样的 再想想 
      

  4.   


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