PHP Methods for implementing Decimal, Binary, octal, and 16 binary conversion related functions

Source: Internet
Author: User
This article mainly introduces the PHP implementation of decimal, binary, octal and 16 binary conversion correlation function usage, combined with concrete instance form more detailed analysis of PHP a variety of common conversion function functions, parameters, use methods and related considerations, the need for friends can refer to the next

1. Binary:

1.1. Binary Turn decimal:

Function:bindec(string $binary_string)

@param $binary The _string parameter represents the binary string to be converted.
@ return returns the value such as the decimal number of the binary number represented by the $binary_string parameter.

Function Description:

bindec()Converts a binary number to an integer type or to a float type for size.
bindec()Interprets all $binary_string values as unsigned integers. This is because the Bindec () function treats its most significant bit as an order of magnitude rather than as a sign bit. "That is, the highest bit 0 or 1 is not represented by BINDEC () as + or-but by a value of 1 is 1,0 0"

Note: The parameter must be a string, and using other data types can cause unpredictable results.

Example:

<?php  Echo bindec (' 10010 '). "\ n";  echo bindec (' 00110 '). "\ n";  echo bindec (' 1111 '). "\ n";

The above program statements will be output in turn: 18,6,15

1.2. Binary Turn hex

Function:bin2hex(string $str)

@param $str The string of ASCII characters that will be converted.
@ return returns the hexadecimal value of the converted string.

Function Description:

bin2hex()function to convert a string of ASCII characters to a hexadecimal value. Strings can be converted back by using the pack () function.
bin2hex()function conversions use byte mode, with a high four-bit word preference.

Example:

(1) bin2hex() convert ' Chengdu ' to hexadecimal value:

<?php  $str = Bin2Hex (' Chengdu ');  Echo $str;

The above program statements will output: 6368656e676475

(2) Convert a string value from binary to 16 and back again:

<?php  $str = ' Chengdu ';  echo Bin2Hex ($STR). "<br/>";  Echo Pack ("h*", Bin2Hex ($STR)). "<br/>";

The above program statement output: 6368656e676475, Chengdu

2. Eight binary:

2.1. Eight binary decimal:

Function:octdec(string $octal_string)

@param $octal The _string parameter represents the octal string that will be converted.
@ return returns the decimal equivalent of the octal number represented by the $octal_string parameter.

Function Description:

octdec()You can handle an integer large number, but in this case it returns a float type.

Example:

<?php  Echo octdec (' 010 '). "\ n";  Echo Octdec (DECOCT (45));

The above program statements will output: 8, 45

3. Decimal:

3.1. Decimal Turn binary:

Function:decbin(int $number)

@param $number The decimal number to convert, the maximum value that can be converted is decimal 4294967295, and its decbin result is a string of 32 1.
@ return returns the binary string converted into decimal digits.

Function Description:

decbin()The maximum decimal value that the function can convert is 4294967295, and the result is a string of 32 1.

Example:

<?php  Echo decbin (10). "\ n";  Echo Decbin (50);

The above program statements will output: 1010, 110010

3.2. Decimal goto octal:

Function:decoct(int $number)

@param $number The decimal number to convert, the maximum value that can be converted is 4294967295 in decimal, and its decoct () result is "37777777777".
@ return returns a string that contains an octal representation of the given parameter of the argument.

Function Description:

decoct()The maximum number of decimal digits the function can convert is 4294967295, and the result is "37777777777".

Example:

<?php  Echo decoct (10). "\ n";  Echo decoct (50);

The above program statements will be output in turn: 12, 62

3.3. Decimal ext. 16 binary:

Function:dechex(int $number)

@param $number The decimal number that will be converted.
@ return returns a string containing the hexadecimal representation of the given parameter.

Function Description:

dechex()The maximum number of decimal values that a function can convert is: php_int_max*2 +/-1, which is a decimal 4294967295 on a 32-bit system with dechex() a result of ffffffff.

Note: The integer type of PHP is signed, but Dechex () can handle unsigned integers only, and negative integers are handled with no symbols.

Example:

<?php  Echo Dechex (10). "\ n";  Echo Dechex (58);

The above program statements are output in turn: A, 3a

4.16 Binary:

4.1.16 Binary Turn binary:

Function: hex2bin(string $data); convert hexadecimal string to binary string

@param $data data in hexadecimal notation.
@ return returns the binary string for the given data or false on failure.

Function Description:

If the hexadecimal string entered is an odd-numbered length or is an invalid hexadecimal string, an e_warning-level error is thrown.

Example:

<?php  $hex = Hex2bin ("6368656e67206475");  Echo $hex;

The above program statements will output: Cheng du

4.2 Hexadecimal to decimal:

Function: hexdec(string $hex_string); convert hexadecimal string to binary string

@param $hex _string The hexadecimal string that will be converted.
@ return returns the decimal number equivalent to the hexadecimal number represented by the $hex_string parameter.

Function Description:

hexdec()Ignores any non-hexadecimal characters it encounters.

Starting with PHP 4.1.0, this function can handle an integer large number, in which case it returns a float type.

Example:

<?php  Var_dump (Hexdec ("See"));  Var_dump (Hexdec ("ee"));  All of the above two outputs: "Int (238)"  Var_dump (Hexdec ("that"));//output "int"  Var_dump (Hexdec ("A0"));//output "int (160)" c5/>//can be seen by the above example: Hexdec () ignores any non-hexadecimal characters it encounters.

5. Base_convert () function for any binary conversions:

Function:base_convert(string $number, int $frombase, int $tobase)

@param $number The number that will be converted.
@param $frombase The binary of parameter #.
@param $tobase will be converted into the binary.
@ return returns a string that contains the binary representation of the symbol in the system.

Function Description:

The $formbase of the $number itself is specified by the system.
Both $formbase and $tobase can only be integer values between 2 and 36 (including 2 and 36).

Note: due to the use of internal "double" or "float" type, the operation of Base_convert () may result in loss of precision in large values.

Example:

<?php  $hexadecimal = ' A37334 ';  Echo Base_convert ($hexadecimal, 2);  Print 101000110111001100110100  echo base_convert ($hexadecimal, +, a);  Print 10711860

The above is the whole content of this article, I hope that everyone's study has helped.


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.