c#与PHP的位移

时间:2015-05-31 01:50:48

标签: c# php

我正在将一个类从C#移植到PHP。我遇到问题的部分通过重复移位来在C#中创建字符串的散列。 PHP代码为前几次迭代提供了相同的输出,但随后开始产生与C#版本完全不同的结果。假设我正在散列的字符串是'ddc74'。

这是C#:

uint hash = 0;

foreach (byte b in System.Text.Encoding.Unicode.GetBytes(s))
{
      hash += b;
      hash += (hash << 10);
      hash ^= (hash >> 6);
}

这是PHP:

$hash = 0;
settype($hash, "integer");

// convert string to unicode array because the c# version uses each unicode byte.
$source = mb_convert_encoding($s, 'UTF-16LE', 'UTF-8');
$source = unpack('C*', $source);
foreach ($source as $b)
{
    var_dump(array('getEightByteHash() b : ' => $b,));

    $hash += $b;
    $hash += ($hash << 10);
    $hash ^= ($hash >> 6);
}

有人能发现我做错了吗? PHP代码在第3次迭代中脱轨。

0 个答案:

没有答案