rajoo.sharma Posted December 14, 2007 Share Posted December 14, 2007 hello everyone, Could you please help me on calculating XOR checksum. The following works fine and gives me 0, which means the data is correct and we know by manually calculating it that it is correct. But I'm not able to implement it using a loop, as I really have no idea how long the value will be, it can be just 4 characters long or 100. Characters means hex values, a set of 2 each. so in a 4 characters long string we have 2 hex values i.e. a01e will be treated as a0 and 1e. Following works fine and gives me 0: function checksum($values){ $stra=substr($values, 0, 2); $strb=substr($values, 2, 2); $strc=substr($values, 4, 2); $strd=substr($values, 6, 2); $stre=substr($values, 8, 2); $strf=substr($values, 10, 2); $strg=substr($values, 12, 2); $strh=substr($values, 14, 2); $stri=substr($values, 16, 2); eval ("\$a = 0x$stra;\$b = 0x$strb;\$c = 0x$strc;\$d = 0x$strd;\$e = 0x$stre;\$f = 0x$strf;\$g = 0x$strg;\$h = 0x$strh;\$i = 0x$stri;"); echo $a^$b^$c^$d^$e^$f^$g^$h^$i; } $values="0191050902040843d1"; checksum($values); Following gives 2162888: function checksum($values){ $stra=substr($values, 0, 2); $i=2; while($i<strlen($values)) { $strb=substr($values, $i, 2); $i+=2; eval ("\$a = 0x$stra;\$b = 0x$strb;"); $c=$a^$b; $stra=$c; } echo $stra; } $values="0191050902040843d1"; checksum($values); Please Help Regards Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.