开发者

bytes convert to float (php)

开发者 https://www.devze.com 2022-12-26 07:46 出处:网络
How can I convert from bytes to float in php? Like in Java int i = (byte3 & 0xff) << 24 | (byte2 & 0xff) &开发者_开发问答lt;< 16 | (byte1 & 0xff) << 8 | byte0 & 0xff;

How can I convert from bytes to float in php? Like in Java

int i = (byte3 & 0xff) << 24 | (byte2 & 0xff) &开发者_开发问答lt;< 16 | (byte1 & 0xff) << 8 | byte0 & 0xff; 
Float.intBitsToFloat(i);


There may be a more direct way, but here you go:

<?php
var_dump(unpack('f', pack('i', 1059760811)));
?>

This is, of course, machine dependent, but I don't know of any machine running PHP that doesn't use IEEE 754 floats.


I don't think php has bytes, does it? When you assign a number to a variable you'll get an variable with a number type

$a = 10; // integer
$f = 1.0; // double
$b = $a + $f; // $b is double


If I'm understanding you correctly, you want to take a raw 32- or 64-bit "integer" value, and force that set of bits to treated as a floating point number instead?

Try the 'pack' and 'unpack' functions

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号