= byte <= 127) * * @param int $i * @return int */ public static function toByte($i) { $i &= 0xff; if ($i < 128) { return $i; } return $i - 256; } /** * Cast to unsigned byte (0 >= short <= 255) * * @param int $i * @return int */ public static function toUnsignedByte($i) { return $i & 0xff; } /** * Cast to short (-32768 >= short <= 32767) * * @param int $i * @return int */ public static function toShort($i) { $i &= 0xffff; if ($i < 32768) { return $i; } return $i - 65536; } /** * Cast to unsigned short (0 >= int <= 65535) * * @param int $i * @return int */ public static function toUnsignedShort($i) { return $i & 0xffff; } /** * Cast to int (-2147483648 >= int <= 2147483647) * * @param int $i * @return int */ public static function toInt($i) { if (PHP_INT_SIZE === 8) { $i &= 0xffffffff; if ($i < 2147483648) { return $i; } return $i - 4294967296; } return $i; } /** * Cast to unsigned int (0 >= long <= 4294967296) * * @param int $i * @return int */ public static function toUnsignedInt($i) { return $i & 0xffffffff; } /** * Cast to long (-9223372036854775808 >= long <= 9223372036854775807) * * @param int $i * @return int */ public static function toLong($i) { $i = (int)$i; if ($i > static::LONG_MAX_VALUE) { throw new \RuntimeException('Invalid long value'); } if ($i < static::LONG_MIN_VALUE) { throw new \RuntimeException('Invalid long value'); } return $i; } }