echo(073032097109032116104101032118101114121032109111100101108032111102032097032109111100101114110032109097106111114032103101110101114097108046);
Essentially, a very large number. Now, why does it output 241872? I know PHP has float handlers. When I remove the leading zero, it functions as expected. What is that leading zero signifying?
If you use a leading zero, the number is interpreted by PHP as an octal number. Thus, a 9 is not a valid part of the number and the parser stops there:
0730320 (base 8)
= 7 * 8^5 + 3 * 8^4 + 3 * 8^2 + 2 * 8^1
= 7 * 32768 + 3 * 4096 + 3 * 64 + 2 * 8 (base 10)
= 241872 (base 10)
A leading zero indicates an octal integer value
Manual:
Warning
If an invalid digit is given in an octal integer (i.e. 8 or 9), the rest of the number is ignored.
Example #2 Octal weirdness
<?php
var_dump(01090); // 010 octal = 8 decimal
?>
So your number gets cut off after 0730320, which is 241872 in decimal.
010 gives you 8 (octal number)
0x10 gives you 16 (hexadecimal number)
Related
I have 24 bit of binary, which is
111010001100001010011000
equals to 15254168
I only "guess" its 24 bit, because binary length is 24.
I would like to generate all 24 bit decimals programmaticly. (C, PHP or Python)
2**24 returns 16.777.216
So there are 16.777.216 other decimals (combinations). How can i generate them?
I can't understand the "range" of 24 bit.
May someone help me on this?
Thanks.
<!-- language: python -->
Is this what you want?
>>> n = 3
>>> result = [bin(k)[2:].rjust(n, '0') for k in xrange(2**n)]
>>> print result
['000', '001', '010', '011', '100', '101', '110', '111']
>>> n = 24
24bits just mean there are 24 bits(zeros or ones) that together create a binary number.
If you want all combinations, or all numbers that can be expressed with 24 bits, it is just range from 0 to 16777215. Why? here is the table in format (binary = decimal):
000000000000000000000000 = 0
000000000000000000000001 = 1
000000000000000000000010 = 2
000000000000000000000011 = 3
....
....
111111111111111111111110 = 16777214
111111111111111111111111 = 16777215
you dont really need to generate anything. You can check the binary to decimal here: http://www.binaryhexconverter.com/binary-to-decimal-converter
Another thing: Sometimes in binary, the leading zeros are omitted. So decimal three is not 000000000000000000000011 but rather just 11. If the length is 24 and first digit is 1, its still just range 8388608 - 16777215
print_r(bin2hex("11111111"));
echo '</br>';
print_r(bindec("11111111"));
Result
131313131313131
255
I want a hexadecimal 16 byte value to do aes encryption.How is the conversion from binary to hex happening in php.I am getting incorrect value using the function.Also when i convert an array of hexadecimal values to string the byte length changes
You get a correct result, it's just not what you want. bin2hex() returns an ASCII string of the hexadecimal representation. A quote from the manual:
Returns an ASCII string containing the hexadecimal representation of str.
So If you want the hexadecimal number you can use this:
print_r(dechex(bindec("11111111")));
The converter to get hexidecimal is dechex(), but it needs a decimal number. To do that we convert you binary string to a decimal number first using bindec() and then pass it into dechex(), e.g:
print_r(dechex(bindec("11111111")));
<?php
$str = "Hello world!";
echo bin2hex($str) . "<br>";
echo pack("H*",bin2hex($str)) . "<br>";
?>
PHP.NET Manual :
http://php.net/manual/en/function.bin2hex.php
Test Your Result : http://www.cs.princeton.edu/courses/archive/fall07/cos109/bc.html
Detailed Explanation:
http://www.computerhope.com/binhex.htm
It's simply 9 * 16 + F where F is 15 (the letters A thru F stand for 10 thru 15). In other words, 0x9F is 159.
It's no different really to the number 314,159 being:
3 * 100,000 (10^5, "to the power of", not "xor")
+ 1 * 10,000 (10^4)
+ 4 * 1,000 (10^3)
+ 1 * 100 (10^2)
+ 5 * 10 (10^1)
+ 9 * 1 (10^0)
for decimal (base 10).
The signedness of such a number is sort of "one level up" from there. The unsigned value of 159 (in 8 bits) is indeed a negative number but only if you interpret it as one.
$y = 013;
echo $y + 5; //this result in 16
I can not figure it out how its ans is 16? Can any one help?
because 013 isn't decimal (base 10). it's octal (base 8). the value in decimal is:
(0 * 8^2) + (1 * 8^1) + (3 * 8^0) = 0 + 8 + 3 = 11
which gives the correct (though unexpected, at least by you) result of 16 when added to 5.
moral of the story: don't prepend a number literal with 0 unless you know what it means
number with leading zero is octal number
like
$a = 0123; // octal number (equivalent to 83 decimal
Integers can be specified in decimal
(base 10), hexadecimal (base 16), or
octal (base 8) notation, optionally
preceded by a sign (- or +).
To use octal notation, precede the
number with a 0 (zero). To use
hexadecimal notation precede the
number with 0x.
$y = 013;
echo $y + 5;
013 is octal number all php integer numbers are octal .
show this link. first.
http://www.ascii.cl/conversion.htm
Could some one please Explain why echo 05000 = 2560 ???
i don't get it , i tried to search over the net i didn't find it somewhere
Thank you
05000 is octal notation, 2560 is decimal notation.
The leading 0 means that the number is in octal (base 8) rather than decimal (base 10).
50008 = 5×83 + 0×82 + 0×81 + 0×80 = 256010
It's a string integer literal in octal notation.
You can do base conversions with base_convert. Example:
echo base_convert("5000", 8, 10); //echoes "2560"
See Positional notation.
I need to be able to get the last digit of a number.
i.e., I need 2 to be returned from: 12.
Like this in PHP: $minute = substr(date('i'), -1) but I need this in Python.
Any ideas
last_digit = str(number)[-1]
Use the % operator:
x = 12 % 10 # returns 2
y = 25 % 10 # returns 5
z = abs(-25) % 10 # returns 5
Python distinguishes between strings and numbers (and actually also between numbers of different kinds, i.e., int vs float) so the best solution depends on what type you start with (str or int?) and what type you want as a result (ditto).
Int to int: abs(x) % 10
Int to str: str(x)[-1]
Str to int: int(x[-1])
Str to str: x[-1]