Unsigned int可以登录php [英] Unsigned int to signed in php
问题描述
看来,在32位操作系统中,ip2long
返回带符号的int,而在64位操作系统中,则返回了unsigned int.
It appears, that in 32bit OS ip2long
returns signed int, and in 64bit OS unsigned int is returned.
我的应用程序在10台服务器上工作,有些是32位的,有些是64位的,所以我需要它们全部以相同的方式工作.
My application is working on 10 servers, and some are 32bit and some are 64bit, so I need all them to work same way.
在PHP文档中,有一个技巧可以使结果始终为未签名,但是由于我的数据库已经充满了数据,因此我希望对其进行签名.
In PHP documentation there is a trick to make that result always unsigned, but since I got my database already full of data, I want to have it signed.
那么如何在PHP中将未签名的int更改为已签名的int?
So how to change an unsigned int into a signed one in PHP?
推荐答案
PHP不支持将无符号整数作为类型,但是您可以做的只是将 sprintf将/ip2long"rel =" noreferrer> ip2long 转换为无符号整数 string 用%u
将该值解释为无符号的:
PHP does not support unsigned integers as a type, but what you can do is simply turn the result of ip2long into an unsigned int string by having sprintf interpret the value as unsigned with %u
:
$ip="128.1.2.3";
$signed=ip2long($ip); // -2147417597 in this example
$unsigned=sprintf("%u", $signed); // 2147549699 in this example
编辑,因为您确实希望甚至在64位系统上也对其进行签名-这是将64位+ ve值转换为等效的32位签名的方法:
Edit, since you really wanted it to be signed even on 64 bit systems - here's how you'd convert the 64 bit +ve value to a 32 bit signed equivalent:
$ip = ip2long($ip);
if (PHP_INT_SIZE == 8)
{
if ($ip>0x7FFFFFFF)
{
$ip-=0x100000000;
}
}
这篇关于Unsigned int可以登录php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!