Re: Working with internal data formats

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 15/05/2008, John Gunther <php@xxxxxxxxxxxxxxxx> wrote:
> Iv Ray wrote:
>
> > John Gunther wrote:
> >  > What technique can I use to take an 8-byte double precision value, as
> >  > stored internally, and assign its value to a PHP float variable without
> >  > having the bytes misinterpreted as a character string.
> >
> > Does it get misinterpreted, or do you just want to be sure?
> >
> > The documentation says -
> >
> > "Some references to the type "double" may remain in the manual. Consider
> double the same as float; the two names exist only for historic reasons."
> >
> > Does this cover your case?
> >
> > Iv
> >
>  No.
>
>  Example: I extract the 8 bytes 40 58 FF 5C 28 F5 C2 8F from an external
> file, which is the internal double precision float representation of the
> decimal value 99.99. Starting with that byte string, how can I create a PHP
> variable whose value is 99.99?

Reversing the order of bytes then using unpack('d') works for me.

<?php
$bytes = pack('H*', '4058FF5C28F5C28F');

$output = unpack('d', strrev($bytes));
print array_shift($output) . "\n";
// 99.99
?>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux