Php converts binary, octal, and hexadecimal formats.

Source: Internet
Author: User
Tags decimal to binary
Php converts binary, octal, and hexadecimal formats.

  1. /**

  2. * Convert decimal to binary, octal, or hexadecimal with zero padding before the digits being insufficient *
  3. *
  4. * @ Param array $ datalist input data array (100,123,130)
  5. * @ Param int $ bin conversion can be: 2, 8, 16
  6. * @ Return array returns the data array () and returns the format in which no data is converted.
  7. * @ Author chengmo QQ: 8292669
  8. * @ Copyright http://www.cnblogs.com/chengmo
  9. */
  10. Function decto_bin ($ datalist, $ bin)
  11. {
  12. Static $ arr = array (, 'A', 'B', 'C', 'D', 'e ', 'F ');
  13. If (! Is_array ($ datalist) $ datalist = array ($ datalist );
  14. If ($ bin = 10) return $ datalist; // ignore in the same hexadecimal format
  15. $ Bytelen = ceil (16/$ bin); // Obtain the length of a byte in $ bin notation.
  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; // The length of less than one byte is automatically increased to 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. }

II,Binary, octal, and hexadecimal to decimalThis conversion uses multiplication, for example: 1101 to decimal: 1*2 ^ 3 + 1*2 ^ 2 + 0*2 ^ 1 + 1*2 ^ 0

  1. /**

  2. * Convert binary, octal, and hexadecimal to decimal *
  3. *
  4. * @ Param array $ datalist input data array (df, ef)
  5. * @ Param int $ bin conversion can be: 2, 8, 16
  6. * @ Return array returns the data array () and returns the format in which no data is converted.
  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; // do not convert to the 10th hexadecimal format
  14. $ AOutData = array (); // defines the output to save the array
  15. Foreach ($ datalist as $ num)
  16. {
  17. $ Atnum = str_split ($ num); // splits the 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 ('20140901', '20160901'), 2 ));
  38. Var_dump (bin_todec (array ('20140901', '20160901'), 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. }

Php actually has many built-in functions to accomplish this: bindec (), decoct (), dechex () base_convert () decbin (). here is just the implementation idea.

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.