PHP version n-base algorithm

Source: Internet
Author: User

 

 

Basic 1 algorithm:

This algorithm can be ~ Converts the hexadecimal value to the hexadecimal value.

Because recursion is used, an array must be provided as the reference variable during the 10-in-10 => N-in conversion process. Save the Conversion Result

 

<? Php <br/>/** <br/> * convert to N in decimal format <br/> * @ Param int $ IX The number of 10 hexadecimal values to be converted <br/> * @ Param int $ number between 2-9 in IM format <br/> * @ Param array $ abuf <br/> * @ return void <br/> */ <br/> function K ($ IX, $ im, & $ abuf) <br/>{< br/> if ($ IX> $ IM) <br/> K (floor ($ IX/$ IM ), $ im, $ abuf); <br/> elseif ($ IX = $ IM) <br/> $ abuf [] = '1 '; <br/> $ abuf [] = $ IX % $ im; <br/>}< br/>/** <br/> * convert N-base to decimal <br/> * @ Param string $ SS The N-base number string to be converted <br/> * @ Param int $ number between im-base 2-9 <br/> * @ return int <br/> */<br /> function rk ($ SS, $ IM) <br/>{< br/> $ ilen = strlen ($ SS)-1; <br/> for ($ I = $ ilen; $ I> = 0; $ I --) <br/> $ ibuf + = POW ($ im, $ I) * intval ($ SS {ABS ($ I-$ ilen)}); <br/> return $ ibuf; <br/>}< br/> $ abuf = array (); <br/> $ inhex = 2; // number between 2 and 9 <br/> $ isource = 129; // number of 10 hexadecimal values to be converted <br/> echo "prerequisites: 2 ≤ n ≥ 9. The current value is ". $ inhex. "hexadecimal result;/N"; <br/> K ($ isource, $ inhex, $ abuf); <br/> echo '10 hexadecimal-> N hexadecimal :'. join ('', $ abuf ). "/N"; <br/> echo 'n' hexadecimal-> 10 hexadecimal :'. rk (join ('', $ abuf), $ inhex); <br/>?> 

 

If you want to use it, indicate the source. Thank you.

 

 

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.