我目前正在开发一个将 VB6 代码转换为 PHP 的项目,我需要一些帮助。我想知道我的校验和函数的转换是否正确,因为我得到的值根本不准确。
VB6 代码:
Function CheckSum(St As String) As Long
Dim A As Long, B As Long
For A = 1 To Len(St)
B = B + Asc(Mid$(St, A, 1))
Next A
CheckSum = B
End Function
到
PHP代码:
Function CheckSum($st)
{
For($a=0; $a<Count($st); $a++)
{
$B = $B + SubStr($st,$a,1);
}
return $B;
}
整个PHP代码:
Function CheckSum($st)
{
For($a=0; $a<Count($st); $a++)
{
$B = $B + SubStr($st,$a,1);
}
return $B;
}
Function DoubleChar($num)
{
$DoubleChar = Chr(IntVal($num / 256)) + Chr($num % 256);
Return $DoubleChar;
}
$host = '127.0.0.1';
$port = 7973;
$waitTimeoutInSeconds = 1;
if($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds))
{
$getplayers = DoubleChar(Chr(35)) + Chr(CheckSum(Chr(35)) * 20 % 194) + Chr(0) + Chr(35);
if(fwrite($fp, $getplayers)):
while (!feof($fp))
{
$buffer = fread($fp, 256);
echo IntVal($buffer);
}
endif;
} else {
echo "ERROR: $errno - $errstr<br />\n";
}
fclose($fp);