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());
}
}
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());
}
}
不过我不懂这个for循环在做什么?for(int i=0;i<=b.length;i++)
{
hex = Integer.toHexString(b[i]&0XFF);
if(hex.length()==1)
{
hex = hex+'0';
}
}
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