java等同于php "fnum"解包函数将十六进制转换为float IEEE 754 in java
java equivalent to php "fnum" unpack function to convert hexadecimal to float IEEE 754 in java
我在PHP中有这个功能:
function hex2float($strHex) {
$array = unpack("fnum", pack("H*", $strHex));
return $array['num'];
}
如何在 java 中写入?
我想以这种方式转换此函数:
private Float hex2float(String hexStr) {
Float result = null;
try {
byte[] decodes = Hex.decodeHex(hexStr.toCharArray());
result = getFloat(decodes, 0);
} catch (DecoderException e) {
// log error
}
return result;
}
private float getFloat(byte[] b, int index) {
int l;
l = b[index + 0];
l &= 0xff;
l |= ((long) b[index + 1] << 8);
l &= 0xffff;
l |= ((long) b[index + 2] << 16);
l &= 0xffffff;
l |= ((long) b[index + 3] << 24);
return Float.intBitsToFloat(l);
}
我在PHP中有这个功能:
function hex2float($strHex) {
$array = unpack("fnum", pack("H*", $strHex));
return $array['num'];
}
如何在 java 中写入?
我想以这种方式转换此函数:
private Float hex2float(String hexStr) {
Float result = null;
try {
byte[] decodes = Hex.decodeHex(hexStr.toCharArray());
result = getFloat(decodes, 0);
} catch (DecoderException e) {
// log error
}
return result;
}
private float getFloat(byte[] b, int index) {
int l;
l = b[index + 0];
l &= 0xff;
l |= ((long) b[index + 1] << 8);
l &= 0xffff;
l |= ((long) b[index + 2] << 16);
l &= 0xffffff;
l |= ((long) b[index + 3] << 24);
return Float.intBitsToFloat(l);
}