使用php将二进制或十六进制字符串转换为32位浮点值。大端词\小端词

使用php将二进制或十六进制字符串转换为32位浮点值。大端词\小端词,php,zend-framework,binary,hex,ieee-754,Php,Zend Framework,Binary,Hex,Ieee 754,如何将32位二进制字符串(如0011100110101000010110000100010或十六进制字符串(如39a85c22)转换为浮点值 用于zend框架。我使用以下类。输出0.000321121013257653次的用法: $x = new Application_Model_Binary("39a85c22"); echo "\n".$x->getFloatFromBits(0, Application_Model_Binary::ENDIAN_BIG); $x = new Ap

如何将32位二进制字符串(如
0011100110101000010110000100010
或十六进制字符串(如
39a85c22
)转换为浮点值


用于zend框架。

我使用以下类。输出
0.00032112101325765
3次的用法:

$x = new Application_Model_Binary("39a85c22");
echo "\n".$x->getFloatFromBits(0, Application_Model_Binary::ENDIAN_BIG);

$x = new Application_Model_Binary("00111001101010000101110000100010", false);
echo "\n".$x->getFloatFromBits(0, Application_Model_Binary::ENDIAN_BIG);

$x = new  Application_Model_Binary("225ca839");
echo "\n".$x->getFloatFromBits(0, Application_Model_Binary::ENDIAN_LITTLE);
班级呢

class Application_Model_Binary
{
    const ENDIAN_BIG = 0;
    const ENDIAN_LITTLE = 1;

    private $bin;

    public function __construct($string, $as_hex = true) {
        if($as_hex){
            $this->bin = $this->hex2bin($string);
        }else{
            $this->bin = $string;
        }
    }

    /**
     * convert hex-string into bin-string
     * @param string $data
     * @return string
     */
    private function hex2bin($data){
        $encoded = '';
        $data_arr = str_split($data, 2);

        foreach($data_arr as $val){
            $binary = base_convert($val, 16, 2);
            $encoded .= str_pad($binary, 8, '0', STR_PAD_LEFT);
        }
        return $encoded;
    }

    /**
     * get integer value out of current binary
     * @param integer $start offset
     * @param integer $length length
     * @return integer
     */
    public function getValueFromBits($start, $length = null){
        return base_convert($this->getBinarySlice($start, $length), 2, 10);
    }

    /**
     * get particular piece of current binary
     * @param integer $start
     * @param integer $length
     * @return string
     */
    public function getBinarySlice($start, $length = null){
        if($length){
            return substr($this->bin, $start, $length);
        }else{
            return substr($this->bin, $start);
        }
    }

    /**
     * bits count
     * @return type
     */
    public function bits(){
        return strlen($this->bin);
    }

    /**
     * get 32-bit float value at particular offset at specific endian type
     * @param integer $offset offset
     * @return float
     */
    public function getFloatFromBits($offset, $mode = self::ENDIAN_BIG){
        if($mode === self::ENDIAN_BIG){
            $sign = $this->getBinarySlice($offset, 1);
            $exp = $this->getBinarySlice($offset + 1, 8);
            $mantissa = "1" . $this->getBinarySlice($offset + 9, 23);
        }else{
            $sign = $this->getBinarySlice($offset + 24, 1);
            $exp = $this->getBinarySlice($offset + 25, 7).$this->getBinarySlice($offset + 16, 1);
            $mantissa = "1" . $this->getBinarySlice($offset + 17, 7) . $this->getBinarySlice($offset + 8, 8) . $this->getBinarySlice($offset, 8);
        }

        $mantissa = str_split($mantissa);
        $exp = bindec($exp) - 127;
        $base = 0;

        for ($i = 0; $i < 24; $i++) {
            $base += (1 / pow(2, $i))*$mantissa[$i];
        }
        return $base * pow(2, $exp) * ($sign*-2+1);
    }
}
类应用程序\u模型\u二进制
{
常数=0;
常数=1;
私人$bin;
公共函数构造($string$as\u hex=true){
如果($as_hex){
$this->bin=$this->hex2bin($string);
}否则{
$this->bin=$string;
}
}
/**
*将十六进制字符串转换为二进制字符串
*@param string$data
*@返回字符串
*/
私有函数hex2bin($data){
$encoded='';
$data\u arr=str\u split($data,2);
foreach($data\u arr as$val){
$binary=base_convert($val,16,2);
$encoded.=str_pad($binary,8,'0',str_pad_LEFT);
}
返回$encoded;
}
/**
*从当前二进制文件中获取整数值
*@param integer$起始偏移量
*@param integer$length
*@返回整数
*/
公共函数getValueFromBits($start,$length=null){
返回base_convert($this->getBinarySicle($start,$length),2,10);
}
/**
*获取当前二进制文件的特定部分
*@param integer$start
*@param integer$length
*@返回字符串
*/
公共函数getBinarySicle($start,$length=null){
如果($长度){
返回substr($this->bin,$start,$length);
}否则{
返回substr($this->bin,$start);
}
}
/**
*比特数
*@返回类型
*/
公共函数位(){
返回strlen($this->bin);
}
/**
*在特定endian类型的特定偏移量处获取32位浮点值
*@param整数$offset
*@返回浮动
*/
公共函数getFloatFromBits($offset,$mode=self::ENDIAN\u BIG){
如果($mode==self::ENDIAN\u BIG){
$sign=$this->getBinarySicle($offset,1);
$exp=$this->getBinarySicle($offset+1,8);
$尾数=“1”。$this->getBinarySicle($offset+9,23);
}否则{
$sign=$this->getBinarySicle($offset+24,1);
$exp=$this->getBinarySlice($offset+25,7)。$this->getBinarySlice($offset+16,1);
$尾数=“1”。$this->getBinarySicle($offset+17,7)。$this->getBinarySicle($offset+8,8)。$this->getBinarySicle($offset,8);
}
$尾数=str_分割($尾数);
$exp=bindec($exp)-127;
$base=0;
对于($i=0;$i<24;$i++){
$base+=(1/pow(2,$i))*$尾数[$i];
}
返回$base*pow(2,$exp)*($sign*-2+1);
}
}

该位字符串是否采用任何特定格式?ieee 754?@MarcB确切地说是ieee754