java中,IEEE754标准(四字节)的字节数据和浮点数之间的转化
(Floatandby。。。
先贴代码
/**java实现IEEE 754标准
* 16进制转float
* @author wp
*
*/
public class Hex2Float {
public static void main(String[] args) {
System.out.println(bytes2Float(ByteConvertUtil.hexStringToBytes("C2480000")));
System.out.println(bytes2float(ByteConvertUtil.hexStringToBytes("C2480000")));
}
/**
* 字节数组转float
* 采⽤IEEE 754标准
* @param bytes
* @return
*/
public static float bytes2Float(byte[] bytes){
//获取字节数组转化成的2进制字符串
String BinaryStr = bytes2BinaryStr(bytes);
/
/符号位S
Long s = Long.parseLong(BinaryStr.substring(0, 1));
//指数位E
Long e = Long.parseLong(BinaryStr.substring(1, 9),2);
//位数M
String M = BinaryStr.substring(9);
float m = 0,a,b;
for(int i=0;i<M.length();i++){
a = Integer.valueOf(M.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 f;
}
/**
* 将字节数组转换成2进制字符串
* @param bytes
* @return
*/
public static String bytes2BinaryStr(byte[] bytes){
StringBuffer binaryStr = new StringBuffer();
for(int i=0;i<bytes.length;i++){
String str = BinaryString((bytes[i] & 0xFF) + 0x100).substring(1);
binaryStr.append(str);
}
String();
}
public static float bytes2float(byte[] bytes){
String hexString = ByteConvertUtil.bytes2HexString(bytes);
Long l = Hex2Float.parseLong(hexString, 16);
Float f = Float.intBitsToFloat(l.intValue());
return f;
}
public static long parseLong(String s, int radix) throws NumberFormatException {
if (s == null) {
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX");
}
if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX"); }
long result = 0;
boolean negative = false;
int i = 0, len = s.length();
long limit = -Long.MAX_VALUE;
long multmin;
int digit;
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Long.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++), radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
}
}
class NumberFormatException extends IllegalArgumentException {
/**
*
*/
private static final long serialVersionUID = 1L;
public NumberFormatException(String s) {
super(s);
static NumberFormatException forInputString(String s) {
return new NumberFormatException("For input string: \"" + s + "\"");
}
}
输出结果:
浮点型变量float-50.0
-50.0
在项⽬中与终端的进⾏数据传输,在遇到的浮点型数据的时候,碰到了java不像整型那样好操作。协议中只提供了标准“IEEE 754”,咦,对于菜鸟学渣来说这是个啥…… 上⽹查:
**
再具体的底层什么协议,先不管了,让我先画个瓢。
**
* 字节数组转float
* 采⽤IEEE 754标准
* @param bytes
* @return
*/
public static float bytes2Float(byte[] bytes){
//获取字节数组转化成的16进制字符串
String BinaryStr = bytes2BinaryStr(bytes);
//符号位S
Long s = Long.parseLong(BinaryStr.substring(0, 1));
//指数位E
Long e = Long.parseLong(BinaryStr.substring(1, 9),2);
//位数M
String M = BinaryStr.substring(9);
float m = 0,a,b;
for(int i=0;i<M.length();i++){
a = Integer.valueOf(M.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 f;
}
/**
* 将字节数组转换成16进制字符串
* @param bytes
* @return
*/
public static String bytes2BinaryStr(byte[] bytes){
StringBuffer binaryStr = new StringBuffer();
for(int i=0;i<bytes.length;i++){
String str = BinaryString((bytes[i] & 0xFF) + 0x100).substring(1);
binaryStr.append(str);
}
String();
}
多次测试没有问题。然后在⽹上还到⼀段代码,在这贴出来。⾃⼰写的代码肯定⼜不好的地⽅,或者是丢失精度等问题,欢迎⼤家批评指正。
不说了,简单记录下,剥削者来了~~~
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论