简体   繁体   English

在 php 中将二进制字符串转换为浮点数

[英]Convert a binary string to float in php

I get through Modbus some 4-Byte Float Values.我通过 Modbus 获得一些 4 字节浮点值。 I get this 4 bytes in an array: Array ( [0] => 67 [1] => 105 [2] => 25 [3] => 154 ) After some Bitconversion I made this: 1000011011010010001100110011010我在数组中得到这 4 个字节: Array ( [0] => 67 [1] => 105 [2] => 25 [3] => 154 ) 经过一些位转换后,我做了这个: 1000011011010010001100110011010

when putting this binary string into a online converter such as https://www.binaryconvert.com/result_float.html?hexadecimal=4369199A I get the value of 2.331E2 which is correct (233.1).将此二进制字符串放入在线转换器(例如https://www.binaryconvert.com/result_float.html?hexadecimal=4369199A)时,我得到正确的值 2.331E2 (233.1)。

However I fail to convert this binary in a PHP float variable.但是我无法将此二进制文件转换为 PHP 浮点变量。 I tried to point the address to a float variable, but did not succeed.我试图将地址指向一个浮点变量,但没有成功。

How can I convert this binary string or the array above into a php float?如何将此二进制字符串或上面的数组转换为 php 浮点数?

Added: The pointer Idea was stupid, since php does not let me define the type of variable.补充:指针Idea很愚蠢,因为php不让我定义变量的类型。 So I tried a different approach using unpack to a float type.所以我尝试了一种不同的方法,使用 unpack 到 float 类型。 But it does not work either:但它也不起作用:

// This represents a 4 byte float read from modbus
$array=array(67,105,25,154);
$array=array_reverse($array);
print_r($array);
for ($i=0;$i<count($array);$i++) {
    $t+=pow(2,$i*8)*$array[$i];
}
echo '<br>Binary: '.decbin($t). ' - This would be the correct Binary for 233.1';
echo '<br>float: ';
print_r(unpack('f',$t));

This code results in:此代码导致:

Array ( [0] => 154 [1] => 25 [2] => 105 [3] => 67 )
Binary: 1000011011010010001100110011010 - This would be the correct Binary for 233.1
float: Array ( [1] => 6.5189725839687E-10 )

No chance to get my 233.1:(没有机会得到我的 233.1:(

I know I come with a possible answer a little bit later but maybe others found useful.我知道稍后我会给出一个可能的答案,但也许其他人觉得有用。 The conversion is a little bit complicated, I worked few hours on it to include in my application.转换有点复杂,我花了几个小时将其包含在我的应用程序中。

Firstly I created a function to get value of the Mantissa, then calculated a float number.首先我创建了一个 function 来获取尾数的值,然后计算了一个浮点数。 I didn't created function for the calculation of the exponent and sign.我没有创建 function 来计算指数和符号。

This example is valid for positive float numbers, for more details read this:此示例对正浮点数有效,有关更多详细信息,请阅读:

HOW REAL (FLOATING POINT) AND 32-BIT DATA IS ENCODED IN MODBUS RTU MESSAGES MODBUS RTU 消息中如何对真实(浮点)和 32 位数据进行编码

And the script:和脚本:

$array=array(67,105,25,154);
$result = pow(2,(((($array[0] * 256 + $array[1]) & 32640) >> 7)-127)) * calcMantissa((($array[1] & 127) << 16) + ($array[2] << 8) + $array[3]+1);
echo $result . "\n";

function calcMantissa($nb) {
    $retValue = 1;
    for ($i = 0; $i < 22; $i++) {
        $retValue = $retValue + (($nb & (1 << (22 - $i))) > 0) / (pow(2,($i+1))) ;
    }
    return $retValue;
}

which gave the following result:结果如下:

233.10000610352 233.10000610352

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM