/**
* 字节数组转int
* 采用IEEE 754标准
*
* @param bytes
* @return float
*/
public int bytesToInt(byte[] bytes) {
// 获取字节数组转化成的2进制字符串
String binaryStr = bytesToBinaryStr(bytes);
// 符号位S
Long s = Long.parseLong(binaryStr.substring(0, 1));
// 指数位E
Long e = Long.parseLong(binaryStr.substring(1, 9), 2);
// 位数M
String length = binaryStr.substring(9);
float m = 0, a, b;
for (int i = 0; i < length.length(); i++) {
a = Integer.valueOf(length.charAt(i));
b = (float) Math.pow(2, i + 1);
m = m + (a / b);
}
Float f = (float) ((Math.pow(-1, s)) * (1 + m) * (Math.pow(2, (e - 127))));
return (int) (f * 100);
}
/**
* 将字节数组转换成2进制字符串
*
* @param bytes
* @return
*/
public String bytesToBinaryStr(byte[] bytes) {
StringBuilder binaryStr = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String str = Integer.toBinaryString((bytes[i] & 0xFF) + 0x100).substring(1);
binaryStr.append(str);
}
return binaryStr.toString();
}
标签:java,754,int,binaryStr,float,bytes,Long,length
From: https://blog.csdn.net/h2004118/article/details/143103068