PHP的ip2long和long2ip函数的实现原理
最近要做个十进制数字的可逆转换做邀请码,一直没搞清楚怎么弄的,实在太复杂了,今天弄IP时想到这个可以进行转换,于是研究了下原理:
主要是自己整理了下:
$ip = '12.34.56.78'; $ips = explode('.', $ip); $result = 0; $result += $ips[0]<<24; $result += $ips[1]<<16; $result += $ips[2]<<8; $result += $ips[3]; echo bindec(decbin($result)); echo '<br/>'; echo bindec(decbin(ip2long($ip))); echo '<br/>'; $str = ''; $str .= intval($result/intval(pow(2, 24))) .'.'; $str .= intval(($result&0x00FFFFFF)/intval(pow(2, 16))) .'.'; $str .= intval(($result&0x0000FFFF)/intval(pow(2, 8))) .'.'; $str .= intval($result&0x000000FF); echo $str; echo '<br/>'; echo long2ip($result);
以上输出的结果为:
203569230 203569230 12.34.56.78 12.34.56.78
这只是实现的一种方法而已,还有其他的办法
我是想用类似的方法,可以直接把一个十进制数字转换为其他十进制数值并可逆,但遇到中间有很多0的数值时发现问题太多了,如 1000100

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

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



Comparing the two screens, the IPS screen is better. After all, the IPS screen has a high angle, the response speed is very fast, and the color reproduction is also very accurate. These characteristics are enough to surpass the miniLED screen, so it is more recommended to use the IPS screen. Which screen is better, miniled or ips? Answer: ips is better. If miniled is compared with ips, ips is better. The IPS screen is a relatively high-end product among LCD panels. The picture is very stable and there is no water mark when touched. Secondly, ips has a high viewing angle, fast response speed, and accurate color reproduction. In the field of design, professionals in this category like to use it. When enjoying high-speed motion pictures, the IPS hard screen can ensure clarity, smoothness and realism

The explode function in PHP is a function used to split a string into an array. It is very common and flexible. In the process of using the explode function, we often encounter some errors and problems. This article will introduce the basic usage of the explode function and provide some methods to solve the error reports. 1. Basic usage of the explode function In PHP, the basic syntax of the explode function is as follows: explode(string$separator,string$stri

Title: Common errors and solutions when using the explode function in PHP In PHP, the explode function is a common function used to split a string into an array. However, some common errors can occur due to improper use or incorrect data format. This article will analyze the problems you may encounter when using the explode function, and provide solutions and specific code examples. Mistake 1: The delimiter parameter is not passed in. When using the explode function, one of the most common mistakes is that the delimiter parameter is not passed in.

In PHP programming, processing strings is a frequently required operation. Among them, splitting and merging strings are two common requirements. In order to perform these operations more conveniently, PHP provides two very practical functions, namely the explode and implode functions. This article will introduce the usage of these two functions, as well as some practical skills. 1. The explode function The explode function is used to split a string according to the specified delimiter and return an array. Its function prototype is as follows: arra

Use the PHP function "explode" to split a string into an array. In PHP development, you often encounter situations where you need to split a string according to the specified delimiter. At this time, we can use PHP's built-in function "explode" to convert string to array. This article will introduce how to use the "explode" function to split a string and give relevant code examples. The basic syntax of the "explode" function is as follows: arrayexplode(

Solution to php explode error: 1. Find and open the erroneous PHP code; 2. Find the explode function part; 3. Modify the code to "dump(explode(',',$str));die;", that is, use Just comma-separate the array.

In the PHP language, there are many basic functions that can help us process strings quickly and efficiently. Among them, the explode function is a very practical string splitting function. It can split a string into arrays according to the specified delimiter, and then perform more flexible string operations. In this article, we will introduce how to use the explode function to split strings in PHP. 1. The format of the explode function. The format of the explode function in the PHP language is as follows: explode(separa

Use the PHP function "explode" to split a string into multiple substrings. In PHP programming, we often encounter situations where we need to split a string into multiple substrings. At this time, PHP provides a very convenient function "explode" that can help us easily implement this function. The syntax of the "explode" function is as follows: arrayexplode(string$delimiter,string$string[,in
