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);
}
分享到:
相关推荐
help for convert float number to binary num
标签:springframework、data、spring、jpa、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明...
It is used to convert binary files to a format suitable for inclusion into a C program. The binary data is placed into a C data structure that may be cut and pasted into program code. It can be used,...
### Java编程基础知识点详解 #### 一、简介 本文档主要介绍了Java编程语言的基础知识,适合初学者入门学习。文档由Y.Daniel Liang撰写,覆盖了Java编程的基本概念、语法结构以及常用的操作方法。 #### 二、控制台...
在这个"java-二叉树binaryTree"主题中,我们将深入探讨二叉树的实现、操作以及相关的算法。 在Java编程语言中,二叉树可以被表示为一个类,这个类通常包含指向左右子节点的引用,以及可能包含的数据。下面是一个...
标签:github、binarywang、weixin、java、common、中文文档、jar包; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明...
Java(TM) Platform SE binary 7.70U I586完整版 Java(TM) Platform SE binary 7.70U I586完整版,JAVA开发必备的SUN公司软件。
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right ...
2023年01月17日 Java SE Development Kit 8, Update 361 (JDK 8u361) Java 8 是广泛使用的稳定版,JDK8将在2030年12月前提供支持! JAVA环境变量配置:(祥见资源说明) 此电脑->右键->属性->高级系统设置->环境变量...
标签:github、binarywang、weixin、java、mp、中文文档、jar包; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...
They provide methods to read and write primitive data types like `int`, `float`, `double`, and `long`. These streams handle type conversions automatically, making it easier to read and write complex ...
BinaryData.dat
vb6 to cs converter per convetger code
标签:github、binarywang、weixin、java、pay、中文文档、jar包; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...
bindata - 将二进制文件转换为 Go const 例子 bindata -o output.go binfile1.jpg binfile2.jpg 或者使用 go generate: //go:generate bindata -o jpegs.go pic1.jpg pic2.jpg pic3.jpg 参数 ...
inData.open("rubbish.dat", ios::out | ios::binary); double array[SIZE] = {1.2, 2.3, 3.4, 4.5, 5.6}; inData.write(reinterpret_cast*>(&array), sizeof(array)); inData.close(); ``` 1. **初始化数组与文件...
Java 9 Data Structures and Algorithms covers classical, functional, and reactive data structures, giving you the ability to understand computational complexity, solve problems, and write efficient ...
public static String decimalToBinary(int n) { StringBuilder binary = new StringBuilder(); while (n > 0) { binary.insert(0, n % 2); n = n / 2; } return binary.toString(); } ``` 这里 `binary....
此 Simulink 模型显示了一个简单的代码,可将十进制数转换为最多 8 位(十进制到 255 位)的二进制数。 然而,它很容易配置,例如,如果需要 9 位,只需更改 Matlab 功能块中的 2 行代码,如下所示: 1) a=零(1,8)...