I am reading values over a (modbusTCP) connection. I get back an array with the following data:
print_r($recData) ;
Output: Array ( [0] => 67 [1] => 100 [2] => 33 [3] => 72 )
how do I get the values back as float? as result I should get the following: 229.718
my code dosent work... the data in $recData shout be a Float Big Endian Value
$i = ($recData[0]<<24) ($recData[1]<<16) ($recData[2]<<8) ($recData[3]);
CodePudding user response:
You may convert it inside PHP like that.
$number = [67, 100, 33, 72];
$strHex = implode('', array_map('dechex', $number));
$v = hexdec($strHex);
$x = ($v & ((1 << 23) - 1)) (1 << 23) * ($v >> 31 | 1);
$exp = ($v >> 23 & 0xFF) - 127;
$float = $x * pow(2, $exp - 23);
echo round($float, 3);
gives
228.13