package com.hdl.sdk.hdl_core.Util.TransformUtil;
|
|
import java.math.BigDecimal;
|
|
/**
|
* Created by JLChen on 2019/7/4
|
*/
|
public class HDLUtlis {
|
|
/**
|
* 将object转为Integer类型
|
*
|
* @param object
|
* @return
|
*/
|
public static Integer getIntegerByObject(Object object) {
|
Integer in = 0;
|
if (object != null) {
|
if (object instanceof Integer) {
|
in = (Integer) object;
|
} else if (object instanceof String) {
|
in = Integer.parseInt((String) object);
|
} else if (object instanceof Double) {
|
in = (int) ((double) object);
|
} else if (object instanceof Float) {
|
in = (int) ((float) object);
|
} else if (object instanceof BigDecimal) {
|
in = ((BigDecimal) object).intValue();
|
} else if (object instanceof Long) {
|
in = ((Long) object).intValue();
|
}
|
}
|
return in;
|
}
|
|
public static float getFloatByObject(Object object) {
|
float f = 0f;
|
try {
|
f = (float)object;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return f;
|
|
}
|
|
|
/**
|
* 4 byte 转换为float类型
|
*
|
* @param b1
|
* @param b2
|
* @param b3
|
* @param b4
|
* @return
|
*/
|
public static float byteToFloat(byte b1, byte b2, byte b3, byte b4) {
|
byte[] mByte = new byte[]{b4, b3, b2, b1};
|
return byteArrayToFloat(mByte);
|
}
|
|
/**
|
* 无符号4Byte整形
|
*/
|
public static float byteToFloat_UnsignedInteger(byte b1, byte b2, byte b3, byte b4) {
|
return (b1 & 0xFF) * 256 * 256 * 256 + (b2 & 0xFF) * 256 * 256 + (b3 & 0xFF) * 256 + (b4 & 0xFF);
|
}
|
/**
|
* 有符号4Byte整形
|
*/
|
public static float byteToFloat_SignedInteger(byte b1, byte b2, byte b3, byte b4) {
|
float mfloat = (b1 & 0x7F) * 256 * 256 * 256 + (b2 & 0xFF) * 256 * 256 + (b3 & 0xFF) * 256 + (b4 & 0xFF);
|
if(((b1 & 0xFF) >> 7) > 0){
|
return -mfloat;
|
}else{
|
return mfloat;
|
}
|
}
|
|
|
/**
|
* byte[4]数组 转换为float类型
|
*
|
* @param arr 长度为4
|
* @return
|
*/
|
public static float byteArrayToFloat(byte[] arr) {
|
try {
|
return Float.intBitsToFloat(getInt(arr));
|
} catch (Exception e) {
|
e.printStackTrace();
|
return 0;
|
}
|
}
|
|
/**
|
* 连续4个字节获得一个int
|
*
|
* @param arr
|
* @return int
|
*/
|
public static int getInt(byte[] arr) {
|
return (0xff000000 & (arr[0] << 24)) |
|
(0x00ff0000 & (arr[1] << 16)) |
|
(0x0000ff00 & (arr[2] << 8)) |
|
(0x000000ff & arr[3]);
|
}
|
|
/**
|
* float转换为byte[4]数组
|
*
|
* @param f
|
* @return
|
*/
|
public static byte[] getByteArray(float f) {
|
int intbits = Float.floatToIntBits(f);//将float里面的二进制串解释为int整数
|
return getByteArray(intbits);
|
}
|
|
/**
|
* byte[4]数组 转换为float类型
|
* @param sign 数据类型 0保留 1无符号4Byte整形 2有符号4Byte整形 3Float形(代±) 注:小数点的用浮点表示,整数不表示小数点
|
* @param b1~b5 sign == 3 的时候 数据值高位在前,低位在后 (数据从后往前装载,例如100的值,应该时0x00000064)
|
* @return
|
*/
|
public static float byteArrayToFloatWithSign(byte sign, byte b1, byte b2, byte b3, byte b4) {
|
try {
|
|
if(sign == 1){//1无符号4Byte整形
|
return byteToFloat_UnsignedInteger(b1, b2, b3, b4);
|
}else if(sign == 2){//2有符号4Byte整形
|
return byteToFloat_SignedInteger(b1, b2, b3, b4);
|
}else if(sign == 3){//3Float形(代±)
|
byte[] mByte = new byte[]{b4, b3, b2, b1};
|
return byteArrayToFloat(mByte);
|
}else{
|
return byteToFloat_UnsignedInteger(b1, b2, b3, b4);
|
}
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
return 0;
|
}
|
}
|
|
}
|