/**
- * Convert decimal to binary, octal, or hexadecimal with zero padding before the digits being insufficient *
- *
- * @ Param array $ datalist input data array (100,123,130)
- * @ Param int $ bin conversion can be: 2, 8, 16
- * @ Return array returns the data array () and returns the format in which no data is converted.
- * @ Author chengmo QQ: 8292669
-
- * @ Copyright http://www.cnblogs.com/chengmo
- */
- Function decto_bin ($ datalist, $ bin)
- {
- Static $ arr = array (, 'A', 'B', 'C', 'D', 'e ', 'F ');
- If (! Is_array ($ datalist) $ datalist = array ($ datalist );
- If ($ bin = 10) return $ datalist; // ignore in the same hexadecimal format
- $ Bytelen = ceil (16/$ bin); // Obtain the length of a byte in $ bin notation.
- $ AOutChar = array ();
- Foreach ($ datalist as $ num)
- {
- $ T = "";
- $ Num = intval ($ num );
- If ($ num = 0) continue;
- While ($ num> 0)
- {
- $ T = $ arr [$ num % $ bin]. $ t;
- $ Num = floor ($ num/$ bin );
- }
- $ Tlen = strlen ($ t );
- If ($ tlen % $ bytelen! = 0)
- {
- $ Pad_len = $ bytelen-$ tlen % $ bytelen;
- $ T = str_pad ("", $ pad_len, "0", STR_PAD_LEFT). $ t; // The length of less than one byte is automatically increased to 0.
- }
- $ AOutChar [] = $ t;
- }
- Return $ aOutChar;
- }
Test:
- Var_dump (decto_bin (array (128,253), 2 ));
- Var_dump (decto_bin (array (128,253), 8 ));
- Var_dump (decto_bin (array (128,253), 16 ));
X-Powered-By: PHP/5.2.0
- Content-type: text/html
Array (2 ){
- [0] =>
- String (8) "10000000"
- [1] =>
- String (8) "11111101"
- }
- Array (2 ){
- [0] =>
- String (4) 0200"
- [1] =>
- String (4) 0375"
- }
- Array (2 ){
- [0] =>
- String (2) "80"
- [1] =>
- String (2) "FD"
- }
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
/**
- * Convert binary, octal, and hexadecimal to decimal *
- *
- * @ Param array $ datalist input data array (df, ef)
- * @ Param int $ bin conversion can be: 2, 8, 16
- * @ Return array returns the data array () and returns the format in which no data is converted.
- * @ Copyright chengmo QQ: 8292669
- */
- Function bin_todec ($ datalist, $ bin)
- {
- 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 );
- If (! Is_array ($ datalist) $ datalist = array ($ datalist );
- If ($ bin = 10) return $ datalist; // do not convert to the 10th hexadecimal format
- $ AOutData = array (); // defines the output to save the array
- Foreach ($ datalist as $ num)
- {
- $ Atnum = str_split ($ num); // splits the string into a single character array.
- $ Atlen = count ($ atnum );
- $ Total = 0;
- $ I = 1;
- Foreach ($ atnum as $ TV)
- {
- $ TV = strtoupper ($ TV );
-
- If (array_key_exists ($ TV, $ arr ))
- {
- If ($ arr [$ TV] = 0) continue;
- $ Total = $ total + $ arr [$ TV] * pow ($ bin, $ atlen-$ I );
- }
- $ I ++;
- }
- $ AOutData [] = $ total;
- }
- Return $ aOutData;
- }
Test:
- Var_dump (bin_todec (array ('FF ', 'ff33', 'cc33'), 16 ));
- Var_dump (bin_todec (array ('20140901', '20160901'), 2 ));
- Var_dump (bin_todec (array ('20140901', '20160901'), 8 ));
X-Powered-By: PHP/5.2.0
- Content-type: text/html
Array (3 ){
- [0] =>
- Int (255)
- [1] =>
- Int (65331)
- [2] =>
- Int (52275)
- }
- Array (2 ){
- [0] =>
- Int (124)
- [1] =>
- Int (508)
- }
- Array (2 ){
- [0] =>
- Int (342099)
- [1] =>
- Int (5345)
- }
Php actually has many built-in functions to accomplish this: bindec (), decoct (), dechex () base_convert () decbin (). here is just the implementation idea. |