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

[url]http://www.captain.at/howto-java-convert-binary-data.php[/url]
[size=medium]
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:
[/size]

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



[b]Here are the functions for converting binary bytes to double, long, int or float
Function for conversion of an 8 byte array to double: [/b]
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);
}


[b]Function for conversion of an 4 byte array to long: [/b]
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;
}




[b]Function for conversion of an 2 byte array to int: [/b]

public static int arr2int (byte[] arr, int start) {
int low = arr[start] & 0xff;
int high = arr[start+1] & 0xff;
return (int)( high << 8 | low );
}



[b]Function for conversion of an 4 byte array to a float: [/b]
	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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值