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