- substr: documentation ( source)
- dechex: documentation ( source)
- hexdec: documentation ( source)
<?php
//DOMANDA: 0x55 0x82 0x30 0x01 0x33 0x33 0x91
//RISPOSTA: 0x55 0x82 0xB0 0x01 0x00 0x05 0x72
function CheckSumCalculation($hex) {
$dsum=0;
for ($i=0; $i < strlen($hex)-1; $i+=2){
$dsum += hexdec($hex[$i].$hex[$i+1]);
}
$NotSum = ~$dsum;
$ChkSum = substr(dechex($NotSum), -2);
return $ChkSum;
}
$LstIA = array("64","65","66","67","68","69","6A","6B","6C");
foreach ($LstIA as &$addr) {
$value = $addr * 2;
$DXPcmd = "558210".$addr."3333";
$DXPcmd .= CheckSumCalculation($DXPcmd);
echo "<BR>".$DXPcmd;
}
?>