Conversion of PHP into binary

Source: Internet
Author: User
Tags binary to decimal

Learn the PHP conversion, there are a lot of knowledge points, logic, but also the most primitive written calculation, but we are accustomed to use code to implement the conversion, the conversion code is as follows: binary (BIN) octal (Oct) decimal (dec) hex (hex)

Examples of specific conversion and use methods and instructions are as follows: Convert binary to decimal bindec, convert decimal to octal decoct, convert decimal to hexadecimal dechex,

The sample code is as follows:

Example 1.

PHP output 20 decimal number, octal number, hexadecimal number */
$i = 20;
printf ("%d", $i);//%d = 10 binary
echo "<br/>";
printf ("%o", $i);//%0 = 8 binary
echo "<br/>";
printf ("%x", $i);//%x represents 16 binary

Example 2.

Write the conversion method between 2, 8, and 16 binary. Write down the following results
(binary number) 11100101011 = (octal number) = (hexadecimal number)
$a =bindec (11100101011);//convert binary 11100101011 to decimal using BINDEC.
echo $a; Output a value of 11100101011 in decimal
echo "<br/>";
echo decoct ($a);//convert decimal to octal using DECOCT.
echo "<br/>";

Example 3.

Write the binary turn 10 binary method and the 10 binary to 2 binary method, write the following results
(binary number) 101011100011001 = (decimal number)
(decimal number) 456 = (binary number
/*echo bindec (' 101011100011001 ');//Remember to add double quotation marks otherwise the result is problematic.
echo "<br/>";
echo decbin (' 456 ');

Conversion of PHP into binary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.