


How to implement functions related to decimal, binary, octal and hexadecimal conversion in PHP
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('10010') . "\n"; echo bindec('00110') . "\n"; echo bindec('1111') . "\n";
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('chengdu'); echo $str;
The above program statement will output: 6368656e676475
(2) Convert a string value from binary to hexadecimal, and then convert it back:
<?php $str = 'chengdu'; echo bin2hex($str) . "<br/>"; echo pack("H*", bin2hex($str)) . "<br/>";
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( '010' ) . "\n"; echo octdec( decoct( 45 ) );
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. .
<?php echo decbin ( 10 ) . "\n"; echo decbin ( 50 );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 );
The above program statements will output in sequence: 12, 62
3.3. Decimal conversion Hexadecimal:Function:
dechex(int $number)@
The decimal number to be converted. @return
Returns a string containing the hexadecimal representation of the given $number parameter.
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><pre class='brush:php;toolbar:false;'><?php
echo dechex ( 10 ) . "\n" ;
echo dechex ( 58 );</pre><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;
以上程序语句会输出: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()会忽略它遇到的任意非十六进制的字符。
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 = 'A37334' ; echo base_convert ( $hexadecimal , 16 , 2 ); //print 101000110111001100110100 echo base_convert ( $hexadecimal , 16 , a); //print 10711860
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools