[英]Converting binary file data to floating-point data in PHP
I would like to store floating point numbers in a SQL blob record and then read in the data as floating point numbers. 我想将浮点数存储在SQL Blob记录中,然后将数据作为浮点数读入。 I am not sure this is possible and I have not been able to make the conversion on my own. 我不确定是否有可能,而且我无法自行进行转换。
For example, suppose I read in the string data "3f000000"
. 例如,假设我读取了字符串数据"3f000000"
。 This 4-byte interpreted as an IEEE-754 floating point number would 0.5. 将此4字节解释为IEEE-754浮点数将为0.5。
I would like to make this conversion in PHP. 我想在PHP中进行此转换。 I tried using $n = floatval(hexdec('3f000000'))
but that just returns an integer number. 我尝试使用$n = floatval(hexdec('3f000000'))
但这只是返回一个整数。 Could someone help me with this please? 有人可以帮我吗?
Also, $n=floatval('0x3f000000')
just returns 0. 同样, $n=floatval('0x3f000000')
仅返回0。
are you sure that "3f000000" is realy 0.5 ? 您确定“ 3f000000”真的是0.5吗?
it try this : 它尝试这样做:
var_dump(unpack("f", pack("H*", "3F000000"))); // give 8.82...E-44
var_dump(unpack("f", pack("H*", "0000003F"))); // give 0.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.