about converting binary numbers of length 64 to decimal in php - php

I tried to convert a 64-bit binary to decimal.
For example with input like this :
"0011000000110000100000011001110011100011000111101110001101001011"
I should obtain this output :
"‭3472417823527985995‬"
But the results obtained are inconsistent with expectations. The result obtained from the example input is
"3472417823527986176",
Here is my code:
<?php
$b = '0011000000110000100000011001110011100011000111101110001101001011';
echo sprintf("%.0f", bindec($b));

Related

Floating number upto two decimal PHP

I know the question is very basic but it seems nothing working for me.
I have a number (either or float or integer) which I want to be formatted upto two decimal point. For this purpose I'm using PHP function number_format but it converts my number to string.
To convert it back to float I am using (float) or floatval(). But these functions just truncates the number after converting it to float from string.
Here is my code
$revenue_sum = array_sum(array_column($val2, 'weighted_revenue')); //23722
$test = number_format($revenue_sum, 2); //"23,722.00"
$test = (float)number_format($revenue_sum, 2); //23.0
$test = floatval(number_format($revenue_sum, 2)); //23.0
I want the $test to be 23722.00 for the $revenue_sum = 23722
If $revenue_sum = 2372.2 the $test should be 2372.20
number_format() function can be used as follows:
echo number_format($revenue_sum, 2,'.',''); // will return 23722.00 for the $revenue_sum = 23722
You are trying to type cast with ',' value, it is truncating the string.
you can try this
<?php echo sprintf("%2.2f", 8900.258); ?>
which will output as
8900.26
If you assign a floating point value to a variable, then it is converted to an internal binary format (usually using IEEE 754). Not all possible values has an internal representation. So while scanning a text, the float is rounded to the nearest possible value. So for example 1.23 is rounded to 1.22999999999999998.
Because of the internal representation, there is no difference between 100 or 1e2 or 100.0 or 100.0000.
And when printing a floating point value without any formatting instruction, PHP guess a good format and rounding some digits. So 1.22999999999999 is displayed as 1.23(may varies on different systems).
In general: As long you are calculating, formatting doesn't matter. It is mostly the best, to ignore the decimal fragments on debugging. But when printing (=converting to text), use functions like format_number() or any of the printf() functions.
To be more pragmatic:

different between bindec($value) and hexdec(bin2hex($value)) PHP

I'have a $value like that var_dump(bin2hex($value)) output = '015180'
I want the convert this value to decimal. So I know my $value is binary, I write those codes:
var_dump(bindec($value));// 0
var_dump(hexdec(bin2hex($value)));//86400
Result is 86400 but why don't the first one returns me 0. what is the different between two of them. Or Am I missing something ?
I'm only quoting one of comments in bin2hex documentation:
bin2hex function is for converting binary data into a hexadecimal string representation. This function is not for converting strings representing binary digits into hexadecimal.If you want that functionality, you can simply do this:
<?php
$binary = "11111001";
$hex = dechex(bindec($binary));
echo $hex;
?>
This would output "f9". Just remember that there is a very big difference between binary data and a string representation of binary.
Author: tehjosh

Why the integer data types hexadecimal, octal and binary are getting converted to decimal type and then displayed in a browser?

I'm using PHP 7.1.11
I've following PHP code :
<?php
$a = 0123; // an octal number (equivalent to 83 decimal)
$b = 0x1A; // a hexadecimal number (equivalent to 26 decimal)
$c = 0b11111111; // a binary number (equivalent to 255 decimal)
var_dump($a); //Output : int(83) int(26) int(255)
var_dump($b); //Output : int(26) int(255)
var_dump($c); //Output : int(255)
?>
My question is why the data contained in integer data types of octal, hexadecimal and binary are getting converted into decimal and then displayed in browser along with the new datatype integer?
Why it's not showing the original values of variables and also not showing their original data type?
Why there is a conversion step and who does this for what reason?
If we take other data types life boolean, float, string nothing like this happen. Everything works as expected then why this weird thing is happening with these integer data types?
There's only one kind of (integer) number, and the format of the number is completely unimportant. The values 052, 42 and 0x2A all represent exactly the same value. PHP doesn't remember how a number was created; it doesn't store that information because it's useless information that would only waste space being stored. Binary, octal and hexadecimal notations are mere convenience affordances to allow you to express numbers in the most suited notation for the context; it's all being parsed to an int of the same value and then output in decimal notation by default.

PHP JSON BigInt encoding

I have array like:
$array = ['id' => '76561198165327575'];
And I need it to work in JavaScript on client side. So I'm trying to encode it with JSON_NUMERIC_CHECK:
json_encode($array, JSON_NUMERIC_CHECK);
And getting result like:
{"id":7.6561198165328e+16}
But it should be:
{"id":76561198165327575}
What is wrong?
(Azure, Windows, 5.6)
JSON_NUMERIC_CHECK basically tells the encoder "If it looks like a number, encode it as a number":
php > $x = '123456789012234567890';
php > echo json_encode($x, JSON_NUMERIC_CHECK);
1.2345678901223e+20
php > echo json_encode($x);
"123456789012234567890"
And since your number exceeds the representable range for an INT on your platform, you get a float instead.
You are exceeding the bounds of integer on your 32-bit system. The documentation describes that when this occurs, the number is converted to a float.
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.
If you encode in JSON objects with large numbers (greater than PHP_MAX_INT), you will always end up getting a floating point value. The only solution is to store them in the object/array as string (that you already) and not use JSON_NUMERIC_CHECK (but convert the string to a number on the client) or write your own encoding routine.

Convert large decimal numbers to hexadecimal

I had to convert a large decimal number to hexadecimal so i use the function dechex.
So my code go like these :
$large_number = '637188198442990866068821375'
$large_hex = dechex($large_number)
but when i print the variable $large
echo $large
i got these value 7fffffffffffffff and i am very sure that i lose some precision.
What can i do to get the full precision in these operation ?
The largest integer that Javascript can handle is 2^52. +/- 9007199254740992
What is JavaScript's highest integer value that a Number can go to without losing precision?
You'll either need to slice up your integer before it hits Javascript or look at one of the Big Integer libraries.

Categories