JAVA: Convert binary data to double, float, int, long

http://www.captain.at/howto-java-convert-binary-data.php

Quite often you have binary data, consisting of e.g. 4 byte float values or 8 byte double values. It saves storage place if you store numeric data in binary form, but reading it back can be a challange, especially in languages where such conversions are not common.

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

你可能感兴趣的:(java,PHP)