I am currently working on a project to convert VB6 code to PHP and I need some help. I would like to know if my conversion of my checksum function is correct because the value that I am getting is not accurate at all.
VB6 Code:
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
TO
PHP Code:
Function CheckSum($st)
{
For($a=0; $a<Count($st); $a++)
{
$B = $B + SubStr($st,$a,1);
}
return $B;
}
The whole PHP code:
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);
The VisualBasic
checksumcode you posted operates over each character of the string st and transforms it to its Ansi code equivalent (theAsc()function). Moreover the VB code uses theLen()function to operate on the string whereas your PHP code is using thecount()function, which operates over an array.In order to achieve the equivalent result in PHP you’ll need to use a similar function such as ord() with strlen() so your checksum function would look something like: