Php decimal, binary, octal, and hexadecimal conversion functions

Source: Internet
Author: User

This article describes in detail the application of the decimal, binary, octal, and hexadecimal conversion functions in php. If you need them, refer to the following.

1. Description of the decimal system Conversion Function
1. Decimal to binary decbin () function, as shown in the following example:

 The Code is as follows: Copy code Echo decbin (12); // output 1100Echo decbin (26); // output 11010Decbin

(PHP 3, PHP 4, PHP 5)
Decbin -- convert decimal to binary
Description
String decbin (int number)
Returns a string containing the binary representation of the given number parameter. The maximum value to be converted is 4294967295 in decimal format, and the result is a string of 32 characters.

2. Decimal to octal decoct () function

 The Code is as follows: Copy code Echo decoct (15); // output 17Echo decoct (264); // output 410Decoct

(PHP 3, PHP 4, PHP 5)
Decoct -- convert decimal to octal
Description
String decoct (int number)
Returns a string containing the octal representation of the given number parameter. The maximum value to be converted is 4294967295 in decimal format, and the result is 37777777777 ".

3. Decimal to hexadecimal dechex () function

 The Code is as follows: Copy code Echo dechex (10); // outputEcho dechex (47); // output 2fDechex

(PHP 3, PHP 4, PHP 5)
Dechex -- convert decimal to hexadecimal
Description
String dechex (int number)
Returns a string containing the hexadecimal representation of the given number parameter. The maximum value to be converted is 4294967295 in decimal format, and the result is "ffffffff ".

Ii. binary system Conversion Functions
1, binary to hexadecimal to bin2hex () function

 The Code is as follows: Copy code \$ Binary = "11111001 ";\$ Hex = dechex (bindec (\$ binary ));Echo \$ hex; // output f9Bin2hex

(PHP 3> = 3.0.9, PHP 4, PHP 5)
Bin2hex -- convert binary data to hexadecimal Representation
Description
String bin2hex (string str)
Returns an ASCII string in hexadecimal notation of the str parameter. The byte mode is used for conversion. The Byte level is higher than the byte level.

2. Convert binary to decimal bindec () function

 The Code is as follows: Copy code Echo bindec ('20140901'); // output 51Echo bindec ('20140901'); // output 51Echo bindec ('20140901'); // output 7Bindec

(PHP 3, PHP 4, PHP 5)
Bindec -- convert binary to decimal
Description
Number bindec (string binary_string)
Returns the decimal value of the binary number represented by the binary_string parameter.
Bindec () converts a binary number to an integer. The maximum number of convertible values is 31-bit 1 or decimal 2147483647. PHP 4.1.0 starts. This function can process large numbers. In this case, it returns the float type.

Iii. Description of octal system Conversion Functions
Octal to decimal dec Dec () function

 The Code is as follows: Copy code Echo octdec ('77 '); // output 63Echo octdec (decoct (45); // output 45Octdec

(PHP 3, PHP 4, PHP 5)
Octdec -- convert octal to decimal
Description
Number octdec (string octal_string)
Returns the decimal equivalent of the octal value represented by the octal_string parameter. The maximum convertible value is 17777777777 or 2147483647 in decimal format. PHP 4.1.0 starts. This function can process large numbers. In this case, it returns the float type.

Hexadecimal to decimal hexdec () function

 The Code is as follows: Copy code Var_dump (hexdec ("See "));Var_dump (hexdec ("ee "));// Both print "int (238 )"Var_dump (hexdec ("that"); // print "int (10 )"Var_dump (hexdec ("a0"); // print "int (160 )"Hexdec

(PHP 3, PHP 4, PHP 5)
Hexdec -- convert hexadecimal to decimal
Description
Number hexdec (string hex_string)
Returns the decimal number equivalent to the hexadecimal number indicated by the hex_string parameter. Hexdec () converts a hexadecimal string to a decimal number. The maximum value to be converted is 7 fffffff, that is, 2147483647 of the decimal number. PHP 4.1.0 starts. This function can process large numbers. In this case, it returns the float type.
Hexdec () replaces all non-hexadecimal characters with 0. In this way, all the zeros on the left are ignored, but the zeros on the right are included.

5. Any hexadecimal conversion base_convert () function

 The Code is as follows: Copy code \$ Hexadecimal = 'a37334 ';Echo base_convert (\$ hexadecimal, 16, 2); // output 101000110111001100110100Base_convert

(PHP 3> = 3.0.6, PHP 4, PHP 5)

Base_convert -- convert a number between any hexadecimal values
Description
String base_convert (string number, int frombase, int tobase)
Returns a string containing the number in tobase format. The number is specified by frombase in hexadecimal notation. Both frombase and tobase can only be between 2 and 36 (including 2 and 36 ). Numbers higher than decimal digits are represented by the letter a-z. For example, a represents 10, B represents 11, and z represents 35.

Related Keywords:
Related Article

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.