PHP enables binary, octal, and hexadecimal conversions to each other

Source: Internet
Author: User
  1. /**

  2. * decimal-to-binary, octal, hex-less digits 0 *
  3. *
  4. * @param array $datalist incoming data array (100,123,130)
  5. * The binary @param int $bin conversion can be: 2,8,16
  6. * @return Array return data array () returns no data conversion format
  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 binary Ignore
  15. $bytelen =ceil (16/$bin); Get the length of a byte if it is a $bin binary
  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; Less than one byte length, auto front 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, octal, hexadecimal to decimal This conversion is multiplied, for example: 1101 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 incoming data array (DF,EF)
  5. * The binary @param int $bin conversion can be: 2,8,16
  6. * @return Array return data array () returns no data conversion format
  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 for 10 binary
  14. $aOutData =array (); Define output Save Array
  15. foreach ($datalist as $num)
  16. {
  17. $atnum =str_split ($num); Splitting a string into a 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. x-powered-by:php/5.2.0

  40. Content-type:text/html

  41. Array (3) {

  42. [0]=>
  43. Int (255)
  44. [1]=>
  45. Int (65331)
  46. [2]=>
  47. Int (52275)
  48. }
  49. Array (2) {
  50. [0]=>
  51. Int (124)
  52. [1]=>
  53. Int (508)
  54. }
  55. Array (2) {
  56. [0]=>
  57. Int (342099)
  58. [1]=>
  59. Int (5345)
  60. }

Copy Code

PHP actually has a few built-in functions to do this: Bindec (), Decoct (), Dechex () Base_convert () Decbin () Here is just the idea of implementation.

  • 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.