vonvertion

本文介绍如何将二进制文件'data.bin'中的浮点数读取到JAVA的float数组中。通过使用FileInputStream和DataInputStream进行文件读取,并提供了一系列转换函数,如arr2float等,用于将字节数组转换为float、double、long和int类型。

 Given you have a file "data.bin" consisting of float numbers with 4 bytes each and you want to read them into a JAVA float array.
You read the file, convert the numbers and write them in a float[] array:

float[] myarray = new float[100000];
try {
   File file = new File("data.bin");
   InputStream is = new FileInputStream(file);
   DataInputStream dis = new DataInputStream( is );
   long length = file.length();
   if (length > Integer.MAX_VALUE) {
      throw new IOException("File is too large");
   } else {
      byte[] bytes = new byte[(int)length];
      int offset = 0;
      int numRead = 0;
      while (offset < bytes.length && 
         (numRead = is.read(bytes, offset, bytes.length-offset) ) >= 0) {
         offset += numRead;
      }
      if (offset < bytes.length) {
         throw new IOException("Could not completely read file "+file.getName());
      }
      dis.close();
      is.close();
      System.out.println("offset="+offset);

      for (int start = 0; start < offset; start = start + 4) {
         myarray[cnt] = arr2float(bytes, start);
         cnt++;
      }
   }
} catch (Exception e) {
   e.printStackTrace();
}
Here are the functions for converting binary bytes to double, long, int or float 

Function for conversion of an 8 byte array to double:
	public static double arr2double (byte[] arr, int start) {
		int i = 0;
		int len = 8;
		int cnt = 0;
		byte[] tmp = new byte[len];
		for (i = start; i < (start + len); i++) {
			tmp[cnt] = arr[i];
			//System.out.println(java.lang.Byte.toString(arr[i]) + " " + i);
			cnt++;
		}
		long accum = 0;
		i = 0;
		for ( int shiftBy = 0; shiftBy < 64; shiftBy += 8 ) {
			accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy;
			i++;
		}
		return Double.longBitsToDouble(accum);
	}
Function for conversion of an 4 byte array to long: 
	public static long arr2long (byte[] arr, int start) {
		int i = 0;
		int len = 4;
		int cnt = 0;
		byte[] tmp = new byte[len];
		for (i = start; i < (start + len); i++) {
			tmp[cnt] = arr[i];
			cnt++;
		}
		long accum = 0;
		i = 0;
		for ( int shiftBy = 0; shiftBy < 32; shiftBy += 8 ) {
			accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy;
			i++;
		}
		return accum;
	}

Function for conversion of an 2 byte array to int:
	public static int arr2int (byte[] arr, int start) {
		int low = arr[start] & 0xff;
		int high = arr[start+1] & 0xff;
		return (int)( high << 8 | low );
	}
Function for conversion of an 4 byte array to a float: 
	public static float arr2float (byte[] arr, int start) {
		int i = 0;
		int len = 4;
		int cnt = 0;
		byte[] tmp = new byte[len];
		for (i = start; i < (start + len); i++) {
			tmp[cnt] = arr[i];
			cnt++;
		}
		int accum = 0;
		i = 0;
		for ( int shiftBy = 0; shiftBy < 32; shiftBy += 8 ) {
			accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy;
			i++;
		}
		return Float.intBitsToFloat(accum);
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值