Home  >  Article  >  Backend Development  >  How to perform hexadecimal conversion and padding in php

How to perform hexadecimal conversion and padding in php

藏色散人
藏色散人Original
2022-10-18 09:17:161566browse

How to perform base conversion and padding in php: 1. Convert binary to octal through binoct(); 2. Convert binary to decimal through bindec(); 3. Convert binary to decimal through binhex() Hexadecimal; 4. Convert octal to binary through octbin(); 5. Convert octal to decimal through octdec(); 6. Convert octal to hexadecimal through octhex(), etc.

How to perform hexadecimal conversion and padding in php

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

As a loser programmer, I don’t have rich experience , just typing code blindly, but one day, I will also become a technical expert. Today I learned several functions for converting between dots and bases

Common bases:

二进制      binary        ----->  bin
八进制      octal          ----->  oct
十进制      decimal      ----->  dec
十六进制   hexadecimal     ----->  hex

php provides functions for converting between several common bases

Convert binary to other bases

binoct();//转为八进制
bindec();//转为十进制
binhex();//转为十六进制

Convert octal to other bases

octbin();//转为二进制
octdec();//转为十进制
octhex();//转为十六进制

Convert decimal to other Base

decbin();//转为二进制
decoct();//转为八进制
dechex();转为十六进制

Convert hexadecimal to other bases

hexbin();转为二进制
hexoct();//转为八进制
hexdec();//转为十六进制

There are so many listed above, but I actually feel that they are of no use at all. For the above functions, you only need to remember bin , oct, dec, hex is enough. If you want to convert a base to b base, that is ab()

A simple example:

把二进制(bin)转为十进制(dec): bindec();

It’s that simple

But this may not meet our needs, so PHP provides us with a function that can truly convert whatever we want: base_convert();

base_convert() This function has three Parameter

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

Give an example:

$hexadecimal = '125458';//十进制
echo base_convert($hexadecimal, 10, 5);//转为五进制

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to perform hexadecimal conversion and padding in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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