注:当前文件中的数据顺序:低位在前、高位在后
Java对十六进制文件的读取,尤其是使用readInt()和readDouble()方法时必须要对数据进行转换,这样才可以避免读到的数据出错。
我们先提供一个数据转换的类,这样可以便于后面的数据转换:
类名:ByteToOther
方法:
public int intFromByte(byte[] temp){
int value = 0;
// value = (((temp[0] & 0xff) << 24) | ((temp[1] & 0xff) << 16) | ((temp[2] & 0xff) << 8) | (temp[3] & 0xff));
value = ((temp[0] & 0xff) | ((temp[1] & 0xff) << 8) | ((temp[2] & 0xff) << 16) | ((temp[3] & 0xff) << 24));
return value;
}
public double doubleFromByte(byte[] temp){
byte tem = 0;
for(int i = 0; i < (temp.length + 1)/2; i ++){
tem = temp[i];
temp[i] = temp[temp.length - 1 - i];
temp[temp.length - 1 - i] = tem;