bytes convert to float (php)
There may be a more direct way, but here you go:
<?php
var_dump(unpack('f', pack('i', 1059760811)));
?>
This is, of course, machine dependent, but I don't know of any machine running PHP that doesn't use IEEE 754 floats.
Convert float to array of bytes in PHP
try to use pack and unpack function:
$f = 26.75;
$ar = unpack("c*", pack("f", $f));
print_r($ar);
Result:
Array
(
[1] => 0
[2] => 0
[3] => -42
[4] => 65
)
you can find a snippet here
unpacking 4 bytes into a float in PHP
You can split the string in groups of 2 hex characters (1 byte), and convert them in decimal values.
Then, you just need to unpack/pack to get the final float.
To use pack()
, you could use the splat operator (...
) to unpack values of the array in function's parameters.
$src = '000080c0';
// get ['00','00','80','C0']
$arr = array_map('hexdec', str_split($src, 2));
// pack as bytes, unpack as float
$float = unpack('f', pack("C*", ...$arr));
var_dump($float[1]); // float(-4)
See a working example.
Converting random byte (0-255) to a float in PHP?
Try the simple linear relationship
$f = $r / 255.0;
where $r is the random byte, and $f is the random float.
This way, when $r=255, $f is 1.0 and when $r=127, $f is 0.498
to get 0.5 for r=127 would require a different relationship.
Converting byte-stream into numeric data-type
Just looked up the code for Zend_Crypt_Math_BigInteger_Bcmath
and Zend_Crypt_Math_BigInteger_Gmp
which deals with this problem:
Using BCmath (Big-Endian)
This is essentially the solution posted by Chad Birch.
public static function bc_binaryToInteger($operand)
{
$result = '0';
while (strlen($operand)) {
$ord = ord(substr($operand, 0, 1));
$result = bcadd(bcmul($result, 256), $ord);
$operand = substr($operand, 1);
}
return $result;
}
Using GMP (Big-Endian)
Same algorithem - just different function names.
public static function gmp_binaryToInteger($operand)
{
$result = '0';
while (strlen($operand)) {
$ord = ord(substr($operand, 0, 1));
$result = gmp_add(gmp_mul($result, 256), $ord);
$operand = substr($operand, 1);
}
return gmp_strval($result);
}
Changing the algorithem to use Litte-Endian byte-order is quite simple: just read the binary data from end to start:
Using BCmath (Litte-Endian)
public static function bc_binaryToInteger($operand)
{
// Just reverse the binray data
$operand = strrev($operand);
$result = '0';
while (strlen($operand)) {
$ord = ord(substr($operand, 0, 1));
$result = bcadd(bcmul($result, 256), $ord);
$operand = substr($operand, 1);
}
return $result;
}
Using GMP (Litte-Endian)
public static function gmp_binaryToInteger($operand)
{
// Just reverse the binray data
$operand = strrev($operand);
$result = '0';
while (strlen($operand)) {
$ord = ord(substr($operand, 0, 1));
$result = gmp_add(gmp_mul($result, 256), $ord);
$operand = substr($operand, 1);
}
return gmp_strval($result);
}
PHP convert String number with whitespace to Float
Most likely you have a non visible character as the thousand separator character. It's more safe to filter all characters except digits and .
when sanitizing the input string. This can be achieved with the preg_replace
function.
Your script can be modified to something like this:
$a = "3 999.99";
$b = "1 500.11";
$aa = floatval(preg_replace('/[^0-9.]/', '', $a));
$bb = floatval(preg_replace('/[^0-9.]/', '', $b));
$c = $aa - $bb;
echo $aa . ' - ' . $bb . ' = ' . $c;
Additionally, it's a bit dangerous and non exact to use floats when handling money in particular. Please have a look at this repository which helps a lot with this problem: https://github.com/moneyphp/money
PHP: convert string to float (only if string represents a float)
You can use is_numeric() function to check variable which might contain a number.
For example:
$a = "1.23";
if (is_numeric($a)) {
$a = (float)$a;
}
$b = "1.2 to 1.3";
if (is_numeric($b)) {
$b = (float)$b;
}
var_dump([
'a' => $a,
'b' => $b
]);
Output
array (size=2) 'a' => float 1.23 'b' => string '1.2 to 1.3'
(length=10)
How to convert byte array to integer in php?
Why not treat it like the math problem it is?
$i = ($ar[3]<<24) + ($ar[2]<<16) + ($ar[1]<<8) + $ar[0];
Format bytes to kilobytes, megabytes, gigabytes
function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
// Uncomment one of the following alternatives
// $bytes /= pow(1024, $pow);
// $bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . ' ' . $units[$pow];
}
(Taken from php.net, there are many other examples there, but I like this one best :-)
Related Topics
Notice: Undefined Index: Http_Referer
Php's Function to List All Objects's Properties and Methods
Postgres Now() Timestamp Doesn't Change, When Script Works
Routes in Codeigniter - Automatically
Call to Undefined Function Password_Hash()
PHP Laravel: No Connection Could Be Made Because the Target MAChine Actively Refused It
How Do We Implement Custom API-Only Authentication in Laravel
How to Join Three Tables in Codeigniter
Laravel Input Facade VS Request Facade
How to Obtain the Last Word of a String
How to Detect Ambiguous and Invalid Datetime in PHP
How to 'JSON_Encode()' Keys from PHP Array
Mongodb Get _Id as String in Find Query
PHP Session Variables Not Being Maintaned
Curl Import Character Encoding Problem