Home > Backend Development > PHP Tutorial > How to implement functions related to decimal, binary, octal and hexadecimal conversion in PHP

How to implement functions related to decimal, binary, octal and hexadecimal conversion in PHP

墨辰丷
Release: 2023-03-27 17:14:01
Original
3168 people have browsed it

This article mainly introduces the usage of functions related to decimal, binary, octal and hexadecimal conversion in PHP. It analyzes the functions, parameters and usage of various common hex conversion functions in PHP in detail based on specific examples. For related precautions, friends in need can refer to the following

1. Binary:

1.1. Binary to decimal conversion:

Function: bindec(string $binary_string)

@param $binary_string The parameter represents the binary string to be converted.
@return Returns the decimal equivalent of the binary number represented by the $binary_string parameter.

Function description:

bindec()Convert a binary number to Integer type or to float type for size needs.
bindec()Interpret all $binary_string values ​​as unsigned integers. This is because the bindec() function treats its most significant bit as the magnitude rather than the sign bit. [That is, the highest bit 0 or 1 is not represented by bind() as or - but by value to represent 1 is 1 and 0 is 0]

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

Example:

<?php
  echo bindec(&#39;10010&#39;) . "\n";
  echo bindec(&#39;00110&#39;) . "\n";
  echo bindec(&#39;1111&#39;) . "\n";
Copy after login

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

1.2. Binary to hexadecimal

Function: bin2hex(string $str)

@param $str The ASCII character to be converted String.
@return Return the hexadecimal value of the converted string.

Function description:

bin2hex() The function converts a string of ASCII characters into a hexadecimal value. Strings can be converted back using the pack() function.
bin2hex() Function conversion uses byte mode, with the high nibble taking precedence.

Example:

(1)bin2hex()Convert 'chengdu' to hexadecimal value:

<?php
  $str = bin2hex(&#39;chengdu&#39;);
  echo $str;
Copy after login

The above program statement will output: 6368656e676475

(2) Convert a string value from binary to hexadecimal, and then convert it back:

<?php
  $str = &#39;chengdu&#39;;
  echo bin2hex($str) . "<br/>";
  echo pack("H*", bin2hex($str)) . "<br/>";
Copy after login

The above program statements are output in sequence: 6368656e676475, chengdu

2. Octal:

2.1 .Octal to decimal:

Function: octdec(string $octal_string)

@param $octal_string The parameter represents the octal to be converted string.
@return Returns the decimal equivalent of the octal number represented by the $octal_string parameter.

Function description:

octdec() can handle Integer large numbers, but in this case it will return float type.

Example:

<?php
  echo octdec( &#39;010&#39; ) . "\n";
  echo octdec( decoct( 45 ) );
Copy after login

The above program statement will output: 8, 45

##3. Decimal:

3.1. Convert decimal to binary:

Function:

decbin(int $number)

@

param $number The decimal number to be converted, the maximum value that can be converted is 4294967295 in decimal, and the decbin result is a string of 32 ones. @
return Returns the binary string after decimal number conversion.

Function description:

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

Example:

<?php
  echo decbin ( 10 ) . "\n";
  echo decbin ( 50 );
Copy after login

The above program statement will output: 1010, 110010

3.2. Decimal to octal conversion :

Function:

decoct(int $number)

@

param $number The decimal number to be converted, what can be converted The maximum value is 4294967295 in decimal, and its decoct() result is "37777777777". @
return Returns a string containing the octal representation of the given $number parameter.

Function description:

##decoct()

The maximum decimal value that the function can convert is 4294967295, and the result is "37777777777". Example:

<?php
  echo decoct ( 10 ) . "\n" ;
  echo decoct ( 50 );
Copy after login

The above program statements will output in sequence: 12, 62

3.3. Decimal conversion Hexadecimal:

Function:

dechex(int ​​$number)

@

param $number

The decimal number to be converted. @return
Returns a string containing the hexadecimal representation of the given $number parameter.

Function description:

dechex()

The maximum decimal value that the function can convert is: PHP_INT_MAX*2 /- 1, in 32 On the bit system, it is 4294967295 in decimal, and the result of dechex() is ffffffff. <p><strong>注意:</strong>PHP的Integer类型是有符号的,但是dechex()只能处理无符号整数,负整数会以无符号来处理。</p><p>范例:</p><p class="jb51code"></p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;?php echo dechex ( 10 ) . &quot;\n&quot; ; echo dechex ( 58 );</pre><div class="contentsignin">Copy after login</div></div><p></p><p>以上程序语句会依次输出:a, 3a</p><p><strong><span style="font-size: medium">4.十六进制:</span></strong></p><p><strong>4.1.十六进制转二进制:</strong></p><p>函数:<code>hex2bin(string $data); 转换十六进制字符串为二进制字符串

@param $data 使用十六进制表示的数据。
@return 返回给定数据的二进制字符串或者在失败时返回FALSE。

函数说明:

如果输入的十六进制字符串是奇数长度或者是无效的十六进制字符串,则会抛出一个E_WARNING级别的错误。

范例:

<?php
  $hex = hex2bin ( "6368656e67206475" );
  echo $hex;
Copy after login

以上程序语句会输出:cheng du

4.2十六进制转十进制:

函数:hexdec(string $hex_string); 转换十六进制字符串为二进制字符串

@param $hex_string 将要转换的十六进制的字符串。
@return 返回与$hex_string参数所表示的十六进制数等值的十进制数。

函数说明:

hexdec()会忽略它遇到的任意非十六进制的字符。

PHP 4.1.0 开始,该函数可以处理 integer大数字,这种情况下,它会返回float类型。

范例:

<?php
  var_dump ( hexdec ( "See" ));
  var_dump ( hexdec ( "ee" ));
  // 上面两个都输出: "int(238)"
  var_dump ( hexdec ( "that" )); // 输出"int(10)"
  var_dump ( hexdec ( "a0" )); // 输出"int(160)"
  //通过上面的例子可以看出来:hexdec()会忽略它遇到的任意非十六进制的字符。
Copy after login

5.任意进制转换的base_convert() 函数:

函数:base_convert(string $number, int $frombase, int $tobase)

@param $number 将要转换的的数。
@param $frombase参数$number的进制。
@param $tobase 将要转换成的进制。
@return 返回一个包含$number以$tobase进制表示的字符串。

函数说明:

$number本身的进制由$formbase来指定。
$formbase和$tobase都只能是2和36(包括2和36)之间的整数值。

注意:由于使用内部的 "double" 或 "float" 类型,base_convert()的操作可能会导致大数值中的精度丢失。

范例:

<?php
  $hexadecimal = &#39;A37334&#39; ;
  echo base_convert ( $hexadecimal , 16 , 2 );
  //print 101000110111001100110100
  echo base_convert ( $hexadecimal , 16 , a);
  //print 10711860
Copy after login

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

php简单实现数组分页的方法_php技巧

php使用ffmpeg获取视频信息并截图的实现方法_php技巧

thinkphp项目部署到Linux服务器上报错“模板不存在”如何解决_php技巧

The above is the detailed content of How to implement functions related to decimal, binary, octal and hexadecimal conversion in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template