PHP implementation of any of the binary mutual Transfer class sharing, PHP implementation of the binary sharing
I did not know that PHP from the Base_convert can be implemented in any conversion between the system, wrote a ....
<?php/** * Binary Conversion class * @author sgf@funcity * @version 2011-02-15 */class hex{private static $element = 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 '); private static $hex _min = 2; private static $hex _max = 36; /** * Conversion * * * Public function conv ($int, $out _hex, $in _hex=10, $use _system=true) {if ($use _system && function_ Exists (' Base_convert ')) {return Strtoupper (Base_convert ($int, $in _hex, $out _hex));} if ($out _hex = = $in _hex) {return $int;} if ($out _hex > Self:: $hex _max | | $out _hex < self:: $hex _min) {return false; if ($in _hex > Self:: $hex _max | | $in _hex < self:: $hex _min) {return false;} $hex _10 = $this->_conv2hex10 ($int, $i N_hex); Return Strtoupper ($this->_conv_hex ($hex _10, $out _hex)); /** * Convert any binary number to 10 digits */Private Function _conv2hex10 ($int, $in _hex) {$int = Strtoupper (Trim ($int)); if ($in _hex==10) {R Eturn $int; }elseif ($in _hex== 2 && Function_exists (' Bindec ') {return Bindec ($int)} elseif ($in _hex== && function_exists (' Hexdec ')) {return He Xdec ($int); } elseif ($in _hex== 8 && function_exists (' Octdec ')) {return Octdec ($int);} $array = Array (); $result = 0; for ($i =0; $i
$v) {$hex 10_value = Array_search ($v, Self:: $element); if ($hex 10_value==-1) {return false; } $result + = Intval (Pow ($in _hex, $k) * $hex 10_value); } return $result; }/** * Replace the 10 binary number with any number of binary numbers */Private Function _conv_hex ($hex _10, $out _hex) {$hex _10 = intval ($hex _10); if ($out _hex==10) {return $hex _10;} else if ($out _hex==2 && function_exists (' Decbin ')) {return Decbin ($hex _10);} elseif ($out _hex ==16 && Function_exists (' Dechex ')) {return Dechex ($hex _10), ElseIf ($out _hex ==8 && function_exists (' decoct ')) {retur N decoct ($hex _10); } $array = Array (); $result = ""; The conversion is made by using the 10 binary number in addition to the arbitrary binary number of the inverted remainder method. do {array_unshift ($array, $hex _10% $out _hex);//The remainder is inserted into the 1th position of the array array. $hex _10 = $hex _10/$out _hex; Division} while ($hex _10>1); foreach ($array as $k) {$result. = self:: $element [$k]; } return $result; }}?>
http://www.bkjia.com/PHPjc/1028304.html www.bkjia.com true http://www.bkjia.com/PHPjc/1028304.html techarticle PHP Implementation of any of the binary mutual Transfer class sharing, PHP implementation of the system before sharing without knowing that PHP from the Base_convert can be implemented in any conversion between the system, wrote a .... php ...