


Example of PHP implementation of hexadecimal and decimal conversion function
Jan 12, 2017 pm 02:57 PMThe example in this article describes how PHP implements the conversion function between hexadecimal and decimal. Share it with everyone for your reference, as follows:
/** * @desc im:十进制数转换成三十六机制数 * @param (int)$num 十进制数 * return 返回:三十六进制数 */ function get_char($num) { $num = intval($num); if ($num <= 0) return false; $charArr = array("0","1","2","3","4","5","6","7","8","9",'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); $char = ''; do { $key = ($num - 1) % 36; $char= $charArr[$key] . $char; $num = floor(($num - $key) / 36); } while ($num > 0); return $char; } /** * @desc im:三十六进制数转换成十机制数 * @param (string)$char 三十六进制数 * return 返回:十进制数 */ function get_num($char){ $array=array("0","1","2","3","4","5","6","7","8","9","A", "B", "C", "D","E", "F", "G", "H", "I", "J", "K", "L","M", "N", "O","P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y","Z"); $len=strlen($char); for($i=0;$i<$len;$i++){ $index=array_search($char[$i],$array); $sum+=($index+1)*pow(36,$len-$i-1); } return $sum; }
Usage examples:
echo "get_char:".get_char(514549)."<br>"; echo "get_num:".get_num('A0ZZ')."<br>";
I hope this article will be helpful to everyone in PHP programming.
Please pay attention to the PHP Chinese website for more articles related to examples of PHP implementation of hexadecimal and decimal conversion functions!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon
