Jump to content

calculating xor checksum


rajoo.sharma

Recommended Posts

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.