-1

我怎么能把这个 PHP 代码翻译成 Java 代码?

protected function readInt24()
{
    $ret = 0;
    if (strlen($this->_input) >= 3)
    {
        $ret  = ord(substr($this->_input, 0, 1)) << 16;
        $ret |= ord(substr($this->_input, 1, 1)) << 8;
        $ret |= ord(substr($this->_input, 2, 1)) << 0;
        $this->_input = substr($this->_input, 3);
    }
    return $ret;
}

$input 是一个非常疯狂的字符串,其中包含 utf 字符(afaik): 8' 左右

4

1 回答 1

0

3ord(substr($this->_input, ..., ...))检索字符串的前三个字符的 ASCII 值。对于每个都有一个左移,然后与返回进行或运算。

这将在 Java 中翻译为:

public static int readInt24(String input) {
int ret = 0;
if (input.length() >= 3) {
    ret = input.charAt(0) << 16;
    ret |= input.charAt(1) << 8;
    ret |= input.charAt(2) << 0;
}
return ret;
}

请注意,PHP 代码会将实例变量截断_input为仅 3 个字符长。

于 2012-09-17T17:36:25.157 回答