0

I need to pass an int value inside a float from java code to php. The reason is that the third-party API that I have to use in between accepts only float values.

In java I have the following code, that works as expected:

int i1 = (int) (System.currentTimeMillis() / 1000L);
float f = Float.intBitsToFloat(t);
int i2 = Float.floatToIntBits(f);
//i1 == i2

Then I pass float value from Float.intBitsToFloat() to the third-party API and it sends a string to my server with float:

"value1":1.4237714E9

In php I receive and parse many such strings and get an array:

{
"value1" => 1.4237714E9, (Number)
"value2" => 1.4537614E9 (Number)
...
}

Now I need to make Float.floatToIntBits() for each element in php, but I'm not sure how. Will these php numbers be 4 bytes long? Or maybe I can somehow get integer while parsing from string? Any suggestions? Thank you in advance!

Tertium
  • 6,049
  • 3
  • 30
  • 51
  • you just make cast like (int)"1.4237714E9". the PHP will convert it automatocly – volkinc Feb 12 '15 at 20:53
  • @volkinc: I don't think so; `intBitsToFloat` is different from a simple cast. (But this may not work generally, as some valid `int`s will be represented as NaN.) – Louis Wasserman Feb 12 '15 at 21:50
  • i see. Will dig, it's an interesting problem, see this post http://stackoverflow.com/questions/2624869/bytes-convert-to-float-php – volkinc Feb 12 '15 at 22:00
  • @volkinc Did you find out how does it work? I mean maybe you can answer what's wrong with int->float bit repack (flot->int works)? (see my answer) – Tertium Feb 13 '15 at 13:35

2 Answers2

0

Thank you, guys! Yes, I forgot about pack/unpack. It's not really an answer, yet it works for my case:

function floatToIntBits($float_val)
{
    $int = unpack('i', pack('f', $float_val));
    return $int[1];
}

But not vice versa! The strange thing:

$i1 = 1423782793;
$bs =pack('i', $i);
$f = unpack('f', $bs);
//array { 1 => 7600419110912} while should be 7.6004191E12 (E replaced with 109?)
//or may be 7600419110000 which also right, but not 7600419110912!

I can't explain this. Double checked on home system and on server (5.5 and 5.4 php) - the same result.

Tertium
  • 6,049
  • 3
  • 30
  • 51
  • Hi , Show me how did you get $i = 1423782793; – volkinc Feb 13 '15 at 16:07
  • I tried 3 ways to get float and all the time it's 7600419110912, so show me how did you get 1423782793 value – volkinc Feb 13 '15 at 16:24
  • @volkinc see my function above: floatToIntBits(7.6004191E12) == 1423782793. The question is that unpack('f', pack('i', $int_val)) returns 7600419110912, but not 7.6004191E12 (760041910000) – Tertium Feb 14 '15 at 10:36
0

Hi the staff that I found you probably will not like:

function FloatToHex1($data)
{
    return bin2hex(strrev(pack("f",$data)));
}
function HexToFloat1($data)
{
    $value=unpack("f",strrev(pack("H*",$data)));

    return $value[1];
}

//usage

echo HexToFloat1(FloatToHex1(7600419100000));

Give the result like 7600419110912

so the 109 is NOT a substitution of E the problem is recalculation of the numbers with float point. It's sounds funny but PHP recalculation bring you the most accurate answer possible. And this is an answer 7600419110912

So read this post for more info https://cycling74.com/forums/topic/probably-a-stupid-question-but/

volkinc
  • 2,143
  • 1
  • 15
  • 19
  • yeah, it looks like some standard incompatibility. I think php Number is double... sometimes. It strange, because it should have 64bit, but you can't store 64-bit long in Number. The good thing is they produce the same integer. For now it's quite enough, thank you for the clarification. – Tertium Feb 16 '15 at 20:42