How can read floats from binary file? My binary file contains float value 93.048126(encoded as 'A418BA42'). When I try to read and echo that float value from file, it's integer value(1119492260) gets echoed. I tried floatval(), but it didn't work.
How to read floats?
What standard encode floats to 'A418BA42' (is it a HEX ?)
Whatever you use to encode, should have a matching decode function.
Even if PHP wasn't used to encode
there may (I am almost sure) be some function for decode.
Search the PHP Manual for Variable Types info (floats)
and maybe some Maths functions.
This one I can not help you with,
Not unless I spend 1-2 hour Study this very subject.
( i have not used floats very much these 6-7 years i have done PHP code.
I am a TEXT & INTEGER man of PHP, we can say)
Link:
http://docs.php.net/float
Follow the links in this paragraph.
And increase your knowledge in using PHP.
Converting to float
For information on converting strings to float, see String conversion to numbers. For values of other types, the conversion is performed by converting the value to integer first and then to float. See Converting to integer for more information. As of PHP 5, a notice is thrown if an object is converted to float.
regars
halojoy
hi again
some more searching and researching
lead me to this article, which mentions C++ conversion to PHP
http://www.dbactive.com/2007/ Now, there’s a function called unpack() which essentially allows to convert different types of data from binary strings to PHP variables
So take a look at these VERY NICE! and USEFUL two complementary functions:
unpack() and unpack()
http://docs.php.net/pack
http://docs.php.net/unpack
Supported formats for pack / unpack are many:
a NUL-padded string
A SPACE-padded string
h Hex string, low nibble first
H Hex string, high nibble first
c signed char
C unsigned char
s signed short (always 16 bit, machine byte order)
S unsigned short (always 16 bit, machine byte order)
n unsigned short (always 16 bit, big endian byte order)
v unsigned short (always 16 bit, little endian byte order)
i signed integer (machine dependent size and byte order)
I unsigned integer (machine dependent size and byte order)
l signed long (always 32 bit, machine byte order)
L unsigned long (always 32 bit, machine byte order)
N unsigned long (always 32 bit, big endian byte order)
V unsigned long (always 32 bit, little endian byte order)
f float (machine dependent size and representation)
d double (machine dependent size and representation)
x NUL byte
X Back up one byte
@ NUL-fill to absolute position
hope you find what you are looking for
halojoy
First of all thanks for your help. Though I used word encoded, float value is not encoded in file. 'A418BA42' is just hexadecimal representation of 32 bit binary data in hex editor. When I select that value in hex editors, it gives me different data types values.
Signed Byte -92
Unsigned Byte 164
Signed Short 6308
Unsigned Short 6308
Signed Long 1119492260
Unsigned Long 1119492260
Float 93.048126
Binary 10100100000110001011101001000010
If you are treating those four bytes as long or dwords then it is very easy to echo their values. My codes can echo int value '1119492260'. But what if I want to echo float value i.e 93.048126 ?
I know the answer is somewhere in pack/unpack functions, but my several attempts to do that are failed. Either they echo '0' or doesn't echo anything. Can you tell me how exactly I should code?