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
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 );
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><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'><?php
echo dechex ( 10 ) . "\n" ;
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;
以上程序语句会输出: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!
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
AI Hentai Generator
Generate AI Hentai for free.
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1378
52
PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian
Dec 24, 2024 pm 04:42 PM
PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati
How To Set Up Visual Studio Code (VS Code) for PHP Development
Dec 20, 2024 am 11:31 AM
Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
How do you parse and process HTML/XML in PHP?
Feb 07, 2025 am 11:57 AM
This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
7 PHP Functions I Regret I Didn't Know Before
Nov 13, 2024 am 09:42 AM
If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op
Explain JSON Web Tokens (JWT) and their use case in PHP APIs.
Apr 05, 2025 am 12:04 AM
JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,
PHP Program to Count Vowels in a String
Feb 07, 2025 pm 12:12 PM
A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total
Explain late static binding in PHP (static::).
Apr 03, 2025 am 12:04 AM
Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases?
Apr 03, 2025 am 12:03 AM
What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.


