Page 1 of 1

Parsing VB files

Posted: Thu Jan 08, 2004 2:45 pm
by newrecruit
I have some software written in VB that saves data to files and I'm writing a script in PHP that parses them for the data.

I have everything working correctly except for when I get to a value saved as a float. ei Field As Single

I've had no problems with As String, As Byte, As Integer, As Long

example of what I have now thats working.

Code: Select all

while(!feof($fp))
{
    $line = fread($fp, 254);

    $string = substr($line, 0, 10);
    $byte = ord(substr($line, 27, 1));
    $integer = ord(substr($line, 57, 1)) + 
                     (ord(substr($line, 58, 1)) * 256);
    $long = number_format(ord(substr($line, 99, 1)) +  
                     (ord(substr($line, 100, 1)) * 256) + 
                     (ord(substr($line, 101, 1)) * 65536) + 
                     (ord(substr($line, 102, 1)) * 16777216));

    sql query 
}

Any ideas how to read a float value?
Thanks

Posted: Thu Jan 08, 2004 5:49 pm
by JAM
[php_man]floatval[/php_man] and [php_man]is_float[/php_man] might be of interest (as well as the ones for as [php_man]inval[/php_man] etc). If that didn't make sence / help, please do share a couple of lines of the data you are importing.

Posted: Thu Jan 08, 2004 10:15 pm
by newrecruit
I was giving this to explain the value I want. If I do this by hand, I can get the right number but is there an easy way to accomplish this in PHP?
Lets say the bytes in the file are 00 00 25 44…..this byte order is called Intel order, which means that the rightmost byte are the most significant.

Writing the value the usual way it will be 44 25 00 00.
Now we are gonna write it as binary.

01000100 00100101 00000000 00000000

the left most bit are the sign bit….its irrelevant for the file because we are never going to have negative values. (if 1 negative, 0 positive)

Then the next 8 bits…(bit 1-9)
10001000 are called exponent.

And the remaining 23 bits are called mantissa (or the “decimal” part).
0100101 00000000 00000000

now for the formula…….

Value = Sign * (1 + mantissa) * 2 ^ (exponent – 127)

In our example it would be

1 * (1 + 1/4 + 1/32 + 1/128) * 2 ^ (136 -127)

This equals 1.2890625 * 2 ^ 9 = 660
So I need to get 44, 25, 00, 00 changed to 660

Posted: Fri Jan 09, 2004 3:00 pm
by newrecruit
for the record, this works.

Code: Select all

$testing = unpack("ffloat",substr($line,162,4));
echo $testingїfloat];