PHP realizes mutual conversion between binary, octal and hexadecimal

WBOY
Release: 2016-07-25 09:04:30
Original
859 people have browsed it
  1. /**

  2. *Add zeros in front of the missing digits when converting from decimal to binary, octal or hexadecimal*
  3. *
  4. * @param array $datalist Incoming data array(100,123,130)
  5. * @param int $bin The converted base can be: 2 ,8,16
  6. * @return array Return data array() Return the format without data conversion
  7. * @Author chengmo QQ:8292669
  8. * @copyright http://www.cnblogs.com/chengmo
  9. */
  10. function decto_bin($datalist,$bin)
  11. {
  12. static $arr=array(0,1,2,3, 4,5,6,7,8,9,'A','B','C','D','E','F');
  13. if(!is_array($datalist)) $datalist= array($datalist);
  14. if($bin==10)return $datalist; //Same base is ignored
  15. $bytelen=ceil(16/$bin); //Get one byte if it is $bin base The length of
  16. $aOutChar=array();
  17. foreach ($datalist as $num)
  18. {
  19. $t="";
  20. $num=intval($num);
  21. if($num===0)continue;
  22. while($num>0)
  23. {
  24. $t=$arr[$num%$bin].$t;
  25. $num=floor($num/$bin);
  26. }
  27. $tlen=strlen($t );
  28. if($tlen%$bytelen!=0)
  29. {
  30. $pad_len=$bytelen-$tlen%$bytelen;
  31. $t=str_pad("",$pad_len,"0",STR_PAD_LEFT).$t ; //If the length is less than one byte, automatically add 0
  32. }
  33. $aOutChar[]=$t;
  34. }
  35. return $aOutChar;
  36. }

  37. Test:

  38. var_dump(decto_bin( array(128,253),2));
  39. var_dump(decto_bin(array(128,253),8));
  40. var_dump(decto_bin(array(128,253),16));

  41. X-Powered -By: PHP/5.2.0

  42. Content-type: text/html

  43. array(2) {

  44. [0]=>
  45. string(8) "10000000"
  46. [1]= >
  47. string(8) "11111101"
  48. }
  49. array(2) {
  50. [0]=>
  51. string(4) "0200"
  52. [1]=>
  53. string(4) "0375"
  54. }
  55. array(2) {
  56. [0]=>
  57. string(2) "80"
  58. [1]=>
  59. string(2) "FD"
  60. }

Copy code

Binary,Binary, octal, hexadecimal to decimalThis conversion uses multiplication, such as: 1101 converted to decimal: 1*2^3+1*2^2+0*2^1+1*2^0

  1. /**

  2. *Binary, octal, hexadecimal to decimal*
  3. *
  4. * @param array $datalist Pass in data array(df,ef)
  5. * @param int $bin The converted base can be: 2, 8, 16
  6. * @return array Return data array() Return the format without data conversion
  7. * @copyright chengmo QQ:8292669
  8. */
  9. function bin_todec($datalist,$bin)
  10. {
  11. static $arr=array('0'=>0 ,'1'=>1,'2'=>2,'3'=>3,'4'=>4,'5'=>5,'6'=>6,' 7'=>7,'8'=>8,'9'=>9,'A'=>10,'B'=>11,'C'=>12,'D' =>13,'E'=>14,'F'=>15);
  12. if(!is_array($datalist))$datalist=array($datalist);
  13. if($bin==10) return $datalist; //No conversion to decimal
  14. $aOutData=array(); //Define the output to save the array
  15. foreach ($datalist as $num)
  16. {
  17. $atnum=str_split($num); //Convert String split into single character array
  18. $atlen=count($atnum);
  19. $total=0;
  20. $i=1;
  21. foreach ($atnum as $tv)
  22. {
  23. $tv=strtoupper($tv);
  24. if(array_key_exists($tv,$arr))
  25. {
  26. if($arr[$tv]==0)continue;
  27. $total=$total+$arr[$tv]*pow($bin,$atlen -$i);
  28. }
  29. $i++;
  30. }
  31. $aOutData[]=$total;
  32. }
  33. return $aOutData;
  34. }

  35. Test:

  36. var_dump(bin_todec(array( 'ff','ff33','cc33'),16));
  37. var_dump(bin_todec(array('1101101','111101101'),2));
  38. var_dump(bin_todec(array('1234123','12341 '),8));

  39. [0]=>
  40. int(255)
  41. [1]=>
  42. int(65331)
  43. [2]=>
  44. int(52275)
  45. }
  46. array(2) {
  47. [0]=> ;
  48. int(124)
  49. [1]=>
  50. int(508)
  51. }
  52. array(2) {
  53. [0]=>
  54. int(342099)
  55. [1]=>
  56. int(5345)
  57. }

  58. Copy code

php actually has many built-in functions to complete these contents: bindec(), decoct(), dechex() base_convert() decbin() This is just an implementation idea.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!