1

As we know, we can use BitConverter.ToSingle() in C# to convert byte[] containing 4 bytes to a float.

byte[] a = {154,153,89,63};
float floatval = BitConverter.ToSingle(a,0);

and we'll see the value of floatval is 0.85;

Also in VC, we can use readfloat() to read a file flow to do the same thing. So, I wonder if PHP has the same function or how can we accomplish this function using PHP.

PS: I've try to look through McroSoft Reference Source for help, but they use the pointer to sovel the problem, which is not supported in PHP.

Thanks!

And very thanks to Rob to give a possible answer. But in that case.

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

It uses 1059760811 the all bytes thing to get the float, but what I have now is 4 bytes, a byte array, as I mentions above

$a = {154,153,89,63}

And I don't know weather there is any connection between byte array and bytes. If there is, please tell me, thanks again!

D.Doe
  • 23
  • 5
  • Thanks, but what I have is a byte array, while they use a whole bytes thing, I don't know how to apply it in my case. – D.Doe Oct 13 '16 at 10:15
  • Oh I'm too stupid,'int i = (byte3 & 0xff) << 24 | (byte2 & 0xff) << 16 | (byte1 & 0xff) << 8 | byte0 & 0xff;' it exactly solve my problem! – D.Doe Oct 13 '16 at 10:20
  • @D.Doe Can you post that as an answer and mark it as accepted? – Rob Oct 13 '16 at 11:14
  • @Rob OK, Ihave marked! thanks again! – D.Doe Oct 15 '16 at 16:25

0 Answers0