PHP converts any number of binary numbers into a 10 binary method

Source: Internet
Author: User
Tags ord
This article mainly introduces the PHP implementation of the arbitrary number of conversion to 10 binary method, involving the PHP system conversion of the relevant skills, very practical value, the need for friends can refer to the next

The example in this paper is about how the PHP implementation converts any number of binary numbers into 10 binary. Share to everyone for your reference. Specific as follows:

PHP converts any number of binary numbers to 10, for example, 8 to 10, and 16 to 10 in binary.

<?php# Show The steps involved in converting a number # from any base (like octal or hex) to base 10# see below for Exa Mples, instructions and copyrightfunction show_convert_to_base_10 ($number, $base) {//If the number contains a decimal co Mponent if (strstr ($number, '. ')) {//Get the integer and decimal components list ($integer, $decimal) = Explode ('. ', $number);}  else {//The number is an integer $integer = $number;}  Print "<b>convert the base $base number $number to a base ten number:</b><blockquote>"; Print "Convert the integer Component ($integer) of the number:<blockquote>"; Compute the value of the integer component//Loop through the integer digit by digit//Reverse the number for easier Handling $integer = Strrev ($integer); $length = strlen ($integer); for ($pos = 0; $pos < $length; + + $pos) {/* PHP lets treat strings and numbers like arrays specify an offset a nd get the character at that position */$digit = $integer[$pos];     Handle character values for digits//(for bases greater than) if (eregi (' [A-z] ', $digit)) {$digit _value =    (Ord (Strtolower ($digit))-Ord (' a ')) + 10;  $digit = "$digit ($digit _value)";  } else {$digit _value = $digit; }//Multiply the current digit by the radix//raised to the power of the current position $result = $digit _value * PO   W ($base, $pos);  Print "Multiply The value of the digit at position $pos by the value of the radix ($base) raised to the power of the   Position ($pos):<br/> ";   Print "$digit * $base <sup> $pos </sup> = $result <br/><br/>"; $sums [] = $result; } print ' </blockquote> ';  if (Isset ($decimal)) {print "Convert the decimal component (0. $decimal) of the number:<blockquote>";  Pad the number with a leading 0 so, we can//start at position 1 $decimal = ' 0 '. $decimal;   $length = strlen ($decimal); for ($pos = 1; $pos < $length; + + $pos) {$digit =$decimal [$pos];  Handle character values for digits//(for bases greater than) if (eregi (' [A-z] ', $digit)) {$digit _value      = (Ord (strtolower ($digit))-Ord (' a ')) + 10;   $digit = "$digit ($digit _value)";   } else {$digit _value = $digit;  }//Multiply the current digit by the radix//raised to the power of the current position $result = $digit _value *    POW (1/$base, $pos); Print "Multiply The value of the digit at position $pos by the value of the 1/radix ($base) raised to the power of T    He position ($pos):<br/> ";    Print "$digit * 1/$base <sup> $pos </sup> = $result <br/><br/>";  $sums [] = $result; } print ' </blockquote> '; } $sums = implode (' + ', $sums);  Eval ("\ $base _10_value = $sums;"); Print "</blockquote>the value of the base $base number $number in base $base _10_value.  <br/> "; Print "This number was derived from the sum of the values of the previous operations ($sums). <br/> <br/> ";}

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

PHP read, judge and use of Session login for database

PHP implementation of Web Caching tool class code and how to use

File and form operation and database operation involved in uploading PHP files

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.