JAVA解释UINT16文件,首先将UINT16文件用byte来读取
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
其实由于UINT16是C或C++的类型,转为JAVA来解释的是话是对应int,占据两个字节,所以需要两个字节两个字节来解释文件。
以下是将拿到的字节转成十进制。
获取字节:
public static byte[] byteCopy(byte[] content,int start,int tstep){
byte[] value = new byte[content.length];
int end = start+ tstep;
value = java.util.Arrays.copyOfRange(content, start, end);
return value;
}
转换成十进制
public static int byte2int(byte res, byte res1) {
// 一个byte数据左移24位变成0x 000000,再右移8位变成0x00 0000
int targets = (res & 0xff) | ((res1 << 8) & 0xff00);
return targets;
}
本文出自 “日常小记” 博客,谢绝转载!
原文:http://2062402.blog.51cto.com/2052402/1902394