Php implements mutual conversion of hexadecimal values, and php conversion of hexadecimal values

Source: Internet
Author: User
Tags decimal to binary

Php implements mutual conversion of hexadecimal values, and php conversion of hexadecimal values

To convert from decimal to other hexadecimal values, the number is used to continuously divide by the number of hexadecimal values to be converted and read the remainder. Connect them together.

<? Php/*** convert decimal to binary, octal, and hexadecimal digits to zero before digits *** @ param array $ datalist input data array (100,123,130) * @ param int $ bin conversion can be:, 16 * @ return array returns the data array () to return the format without data conversion */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 $ bytelen = ceil (16/$ bin) in the same hexadecimal format; // obtain the length of one byte $ aOutChar = array () If $ bin is used (); 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. The value 0 is automatically added before.} $ 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.0Content-type: text/htmlarray(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"}

Binary, octal, and hexadecimal to decimal

This conversion uses multiplication, for example: 1101 to decimal: 1*2 ^ 3 + 1*2 ^ 2 + 0*2 ^ 1 + 1*2 ^ 0

Code:

<? Php/*** convert binary, octal, and hexadecimal to decimal *** @ param array $ datalist input data array (df, ef) * @ param int $ bin conversion can be in the format of, 16 * @ return array returned data array () returned format without data conversion * @ 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 $ aOutData = array (); // define the output to save the array foreach ($ datalist as $ num) {$ atnum = str_split ($ num ); // split 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('1101101','111101101'),2));var_dump(bin_todec(array('1234123','12341'),8));X-Powered-By: PHP/5.2.0Content-type: text/htmlarray(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)}

These are just implementation methods. In fact, php or other languages are not concerned, and the Implementation ideas are the same. Php actually has many built-in functions to accomplish this:

Bindec (), decoct (), dechex () base_convert () decbin () is just the implementation idea here. Haha!

Articles you may be interested in:
  • Php implements hexadecimal conversion (binary, octal, and hexadecimal) to implement code conversion.
  • PHP function description: decimal, binary, octal, and hexadecimal conversion functions
  • Analysis on the Conversion principle of Chinese Characters in PHP
  • Php Image Binary Conversion Implementation Method

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.