Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#vs PHP中的位移位_C#_Php - Fatal编程技术网

c#vs PHP中的位移位

c#vs PHP中的位移位,c#,php,C#,Php,我正在将一个类从C#移植到PHP。我遇到麻烦的部分是通过反复移位位来创建C#中字符串的散列。PHP代码在最初的几次迭代中为我提供了相同的输出,但随后开始产生与C版本截然不同的结果。假设我正在散列的字符串是“ddc74” 这是C#: uint散列=0; foreach(System.Text.Encoding.Unicode.GetBytes(s)中的字节b) { hash+=b; 散列+=(散列>6); } 下面是PHP: $hash = 0; settype($hash, "integer"

我正在将一个类从C#移植到PHP。我遇到麻烦的部分是通过反复移位位来创建C#中字符串的散列。PHP代码在最初的几次迭代中为我提供了相同的输出,但随后开始产生与C版本截然不同的结果。假设我正在散列的字符串是“ddc74”

这是C#:

uint散列=0;
foreach(System.Text.Encoding.Unicode.GetBytes(s)中的字节b)
{
hash+=b;
散列+=(散列>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);
}
$hash=0;
settype($hash,“integer”);
//将字符串转换为unicode数组,因为c#版本使用每个unicode字节。
$source=mb_convert_编码($s,'UTF-16LE','UTF-8');
$source=unpack('C*',$source);
foreach(来源为$b)
{
变量转储(数组('getEightByteHash()b:'=>$b,);
$hash+=$b;
$hash+=($hash>6);
}

谁能看出我做错了什么?PHP代码在第三次迭代中就出轨了。

在PHP中,您将其设置为整数,在c#中设置为uint(32位无符号),也许就是这样?由于PHP不支持无符号值,因此可能必须使用64位整数,除非PHP整数已经是64位,它非常依赖于平台,就像C/C++一样。在执行位移位或屏蔽时,需要考虑机器端性。我想到了uint vs integer,还尝试了PHP端的float,得到了相同的结果。至于endianess,它们都是基于Intel的机器。C#在64位Windows上运行,PHP在64位Linux上运行。我发现当$hash值超过32位整数时,PHP代码失败。这很奇怪,因为代码在64位机器上运行。显然,PHP不能对大于32位的整数进行逐位运算。有人知道如何绕过这个限制吗?
$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);
}