0
In Javascript I could do so:
let settings = {
valorFloat : file.getFloat64(38, false), //Le o dado da posição 38 no formato float 64 bits
};
newFile.setFloat32(38, file.settings.valorFloat, true); //Insere o dado na posição 38 de newFile com o dado convertido para float de 32 bits
How could I do something similar in PHP? That is, take a data that is written in a hexadecimal format file as a 64-bit float and convert this data to be written in a new file also in hexadecimal format as a 32-bit float. An example:
When reading the data in the file that represents the decimal "0.39" we have the string below with the hexadecimal number that represents the "0.39" in 64 bit format:
0x3fd8f5c28f5c28f6
For this same real number "0.39", the hexadecimal representation in 32-bit format looks like this:
0x3ec7ae14
As you can see, it is not enough just to despise the least significant bits to have the same real number with less precision. It is necessary to perform a calculation to make this conversion, so I have already researched PHP does not have a function ready to do this conversion, does anyone know how this calculation is done? Then I could do the function for this conversion myself.
In PHP you can use unpack() and pack() to convert numbers from/to binary format (bytes) and interpret binary structures contained in files. The article https://adayinthelifeof.nl/2010/01/14/handling-binary-data-in-php-with-pack-and-unpack/ illustrates the reading of a PNG image file.
– epx
Very good article, a pity that this function supports the float format depending only on the machine, it does not allow to specify if I will work with 32 or 64 bits. " g float (machine dependent size, little endian byte order)"
– Veronezi
It seems to me that PHP 7.2 supports both float (32 bits) and double (64 bits) and several endians. See the manual of unpack() https://www.w3schools.com/php/func_misc_unpack.asp
– epx
yes supports both, but all options for float and doble are "machine dependent", ie depend on the machine that are running, whether 32 or 64 bits. I’m brushing the bits here, I found a material that explains about the composition of the real numbers (exponent, mantissa, etc...) I think I’ll be able to solve, then I do a post of the result here if it works...
– Veronezi