How to solve the problem of generating a negative number when the PHP function ip2long is converted to an IP address when the value is too large

Source: Internet
Author: User
Tags binary to decimal

[Cause]: Because PHP's integer type is signed, and your IP addresses will result in negative integers.
[Solution]: As mentioned in its official manual, you can "you need to use the" % u "formatter of sprintf () or printf () to get the string representation of the unsigned IP address"
That is, printf ('% U', ip2long ('IP address '));
Or convert it to binary first and then convert it to decimal,Bindec(Decbin(Ip2long('IP address ')));
Test]
$ StrIp = '1970. 118.0.0 ';

Echo ip2long ($ strIp); // The-1233780736 output at this time
Echo '<br/> ';
Echo bindec (decbin (ip2long ($ strIp); // output 3061186560, consistent with MySQL function output ~

[Note ]:
NumberBindec(String $ binary_string); // convert binary to decimal
StringDecbin(Int $ number); // convert to binary in decimal format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.