Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XOR checksum

Status
Not open for further replies.

boppen

Technical User
Joined
Mar 25, 2006
Messages
2
Location
SE
Hi!

I'm getting a string from a device that's connected on a serial-port that looks like this;

STX Text text text ... CR ETX BCC

Where STX=0x02, ETX=0x03, CR=0x0d and BCC is the checksum 0x00-0xFF.

The checksum should be calculated using XOR of everything exept STX and BCC.

But how should I do that using Perl? I've found lots of C examples but no Perl ... can I just XOR character by character or do I need to do some XOR bit by bit?

I'm using Device::Serialport and I've got everything in a string, and then I'm using a for-loop and substr to get character by character - I don't know if that's the correct way to do it.

Thanks för any help!

Best regards, Lars Fredriksson

 
Hi,
I'm not sure about it but you can give this a try...

Code:
my $txt = 'nice text';
my $xor = 0x03 ^ 0x0d;
for ( split(//, $txt) ) { $xor ^= ord($_); };
print sprintf ("%.32b, %s", $xor, $xor);

you don't have to do this bit by bit AFAIK, 10 ^ 12 works off the bat.

---
cheers!
san
smoking3vc.gif


print length "The answer to life, universe & everything!
 
Hi!

Thanks for the answer, I think it works just as I hope - I will try it for real on Monday!
I think that ord() did the trick that I've missed ;-)

/Boppen
 
glad to be of help.. just out of curiosity, what is this a part of? [pipe]

[ BTW no worries if its under a NDA, etc. ]





---
cheers!
san
smoking3vc.gif


print length "The answer to life, universe & everything!
 
Status
Not open for further replies.

Similar threads

  • Locked
  • Question Question
Replies
4
Views
132
  • Locked
  • Question Question
Replies
1
Views
81

Part and Inventory Search

Sponsor

Back
Top