可靠的整数类型转换 [英] Reliable integer typecasting

查看:131
本文介绍了可靠的整数类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个用于PHP的数据验证程序包(主要是在5.3中的一些新增加的练习,如命名空间)。我计划有一个主要的验证器类,它使用插件类来做实际的验证。

I'm working on a data validator package for PHP (mostly as an exercise in some of the new additions in 5.3 such as namespaces). I'm planning to have a main validator class which uses plugin classes to do the actual validation.

我建立的第一个验证插件只是一个简单的整数。源码如下:

The first validation plugin I'm building is just a simple one for integers. The source is below:

namespace validator\validatorplugins;

class Integer implements ValidatorPlugin
{
    private
        $field  = NULL;

    public function cast ()
    {
        return ($this -> field  = (int) ($this -> field * 1));
    }

    public function isValid ()
    {
        if (!$isValid = is_int ($this -> field * 1))
        {
            $this -> field  = NULL;
        }
        return ($isValid);
    }

    public function __construct ($field)
    {
        $this -> field  = $field;
    }

    public function __toString()
    {
        return ((string) $this -> field);
    }
}



我使用以下测试:

I'm using the following tests of the above class:

$a  = new Integer (0xDEADBEEF);
var_dump ($a -> isValid ());
var_dump ($a -> cast ());
echo ($a . "\n\n");

$b  = new Integer ('0xDEADBEEF');
var_dump ($b -> isValid ());
var_dump ($b -> cast ());
echo ($b . "\n\n");

$c  = new Integer (0777);
var_dump ($c -> isValid ());
var_dump ($c -> cast ());
echo ($c . "\n\n");

$d  = new Integer ('0777');
var_dump ($d -> isValid ());
var_dump ($d -> cast ());
echo ($d . "\n\n");

从上面的测试用例我得到以下结果:

From the above test cases I'm getting the following results:


bool(true)int(3735928559)3735928559

bool(true) int(3735928559) 3735928559

bool(true)int(3735928559)3735928559

bool(true) int(3735928559) 3735928559

bool(true)int(511)511

bool(true) int(511) 511

bool(true)int(777)777

bool(true) int(777) 777

正如你所看到的,我遇到了一个编码八进制数字的字符串的问题。它应该评估为511(0777在10基数),但我实际上得到777而不是。

As you can see, I've run into an issue with a string that encodes an octal number. It should evaluate to 511 (0777 in base 10), but I'm actually getting 777 instead.

由于我有一天想要使用这个类来验证表单,并且作为表单总是被PHP处理为字符串数组,你可以看到处理八进制数偶然是这个插件的字符串有问题。

As I might one day want to use this class to validate forms, and as forms are always treated as arrays of strings by PHP, you can see that processing octal numbers that happen to be strings with this plugin is problematic.

我尝试了各种方法来整数验证(使用is_int总是返回false的字符串,乘以1和is_numeric不会捕获浮点数,typecasting和intval产生不想要的结果当处理八进制数时)。有没有其他方法,我可以使用,将工作基地10(十进制),基地16(十六进制)和基地8(八进制)?

I've tried various approaches to integer validation (using is_int always returns false for strings, multiplying by 1 and is_numeric will not catch floats, typecasting and intval produce an unwanted result when dealing with octal numbers). Is there some other approach I could use that would work for base 10 (decimal), base 16 (hex) and base 8 (octal)?

推荐答案

intval 有一个可选参数 $ base

intval('0777', 8); // 511
intval('0777', 10); // 777
intval('0777'); // 777
intval(0777); // 511

所以你可以这样做:

$i = '0777';

$base = 10;
if (strtolower(substr($i, 0, 2)) === '0x') $base = 16;
else if (substr($i, 0, 1) === '0') $base = 8;

intval($i, $base);

这篇关于可靠的整数类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆